Page 1 of 1

Inline assembly modifier "dN"

Posted: Thu Dec 06, 2012 8:47 am
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.

Re: Inline assembly modifier "dN"

Posted: Thu Dec 06, 2012 9:32 am
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.

Re: Inline assembly modifier "dN"

Posted: Thu Dec 06, 2012 11:02 am
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.

Re: Inline assembly modifier "dN"

Posted: Thu Dec 06, 2012 3:42 pm
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?

Re: Inline assembly modifier "dN"

Posted: Thu Dec 06, 2012 6:43 pm
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".