Inline assembly modifier "dN"

Programming, for all ages and all languages.
Post Reply
DeepakRaj
Posts: 4
Joined: Thu Nov 29, 2012 3:23 am

Inline assembly modifier "dN"

Post by DeepakRaj »

Why do we use dN in inline asm, as in following code.

static inline
void outb( unsigned short port, unsigned char val )
{
asm volatile( "outb %0, %1"
: : "a"(val), "Nd"(port) );
}

wiki says about it as " "Nd" allows for one-byte constant values to be assembled as constants, freeing the edx register for other cases", I will be thankful if somebody can elaborate on this.

Sorry if it's a silly or even if it's the obvious one.

Thanks.
Last edited by DeepakRaj on Thu Dec 06, 2012 11:05 am, edited 1 time in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Inline assembly modifier "dN"

Post by Combuster »

Please read the forum rules and don't use coloured text - I had to copypaste it over to make it legible on my screen.

----

There are three basic opcodes for port inputs and outputs:
1) OUT AL/AX/EAX, DX
2) OUT AL/AX/EAX, immediate
3) OUTS
Each of them come in 8, 16 and 32-bit versions making 9 outs and 9 ins.

The second one only works if the immediate value is exactly one unsigned byte (0-255) while the first one takes the full 16 bits at the cost of a register.

The "Nd" modifier says to either substitute dx (loaded with the right value), or an 8-bit immediate. If it knows the value at compile time, it can check if it qualifies for the N constraint, and fill it out with the actual number. If it doesn't know if it's an 8-bit number (because it can be 16 bits) it will have to resort to using the second option, dx, and then proceeds to load the value it at some point before it. The assembler will see the out with either a small number or dx, and will consequently happily proceed in substituting the right machine code for it.

Thus with one statement you can encode both versions 1 and 2 and have the compiler automatically choose the most efficient one.

Regarding version 3, it's typically used for mass transfers only, so you'll want to use that one only explicitly.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
DeepakRaj
Posts: 4
Joined: Thu Nov 29, 2012 3:23 am

Re: Inline assembly modifier "dN"

Post by DeepakRaj »

Combuster wrote:Please read the forum rules and don't use coloured text - I had to copypaste it over to make it legible on my screen.

----

There are three basic opcodes for port inputs and outputs:
1) OUT AL/AX/EAX, DX
2) OUT AL/AX/EAX, immediate
3) OUTS
Each of them come in 8, 16 and 32-bit versions making 9 outs and 9 ins.

The second one only works if the immediate value is exactly one unsigned byte (0-255) while the first one takes the full 16 bits at the cost of a register.

The "Nd" modifier says to either substitute dx (loaded with the right value), or an 8-bit immediate. If it knows the value at compile time, it can check if it qualifies for the N constraint, and fill it out with the actual number. If it doesn't know if it's an 8-bit number (because it can be 16 bits) it will have to resort to using the second option, dx, and then proceeds to load the value it at some point before it. The assembler will see the out with either a small number or dx, and will consequently happily proceed in substituting the right machine code for it.

Thus with one statement you can encode both versions 1 and 2 and have the compiler automatically choose the most efficient one.

Regarding version 3, it's typically used for mass transfers only, so you'll want to use that one only explicitly.
I apologies for the inconvenience caused and also for violating forum rule.
I'm removing the color part so that it won't be inconvenient for anyone in future, if he/she refers.
Thanks for the details. It's very clear and helpful. An important detail, it is.
Thanks once again.
User avatar
Kazinsal
Member
Member
Posts: 559
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:

Re: Inline assembly modifier "dN"

Post by Kazinsal »

Combuster wrote:Please read the forum rules and don't use coloured text - I had to copypaste it over to make it legible on my screen.
Out of curiosity, Combuster, what combination of screen and forum theme are you using that seems to universally make coloured text illegible for you?
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: Inline assembly modifier "dN"

Post by Brynet-Inc »

Blacklight wrote:
Combuster wrote:Please read the forum rules and don't use coloured text - I had to copypaste it over to make it legible on my screen.
Out of curiosity, Combuster, what combination of screen and forum theme are you using that seems to universally make coloured text illegible for you?
The MegaTokyo theme is popular amongst many of the older members. I don't know if that's what he uses, but that's beside the point.

The existence of other themes has the effect of making text colours "universally illegible".
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Post Reply