Input/output operands in GCC inline assembly
Posted: Sat Jun 07, 2014 1:12 pm
I am writing the inb and outb functions for port I/O.
As part of this, I learnt the basics of inline assembly from various online sources and wrote the following implementation for inb:
Here are my questions:
1. Here is my understanding of how this works:
* %1 is replaced with either the immediate value (because of "N") or edx(because of "d"). The immediate/edx get the value from __port.
* %0 is replaced with eax. (because of "a"). I'm not at all sure about this. Since "a" is only an output operand, will %0 be replaced with eax ?
* The instruction is executed.
* The value from eax is copied to portval.
Is this correct ?
2. Since %0 is always referring to eax, will this implementation be equivalent:
3. What difference does it make if I don't use "N" ? Wouldn't simply using "d" be better, since that is 16 bits ?
4. If I omitted the "N", then is it correct that value of __port is copied to edx and %1 is replaced by edx ?
5. The GCC manual says "N" is
As part of this, I learnt the basics of inline assembly from various online sources and wrote the following implementation for inb:
Code: Select all
__asm__ __volatile__ ("in %0, %1":"=a"(portval):"Nd"(__port));
1. Here is my understanding of how this works:
* %1 is replaced with either the immediate value (because of "N") or edx(because of "d"). The immediate/edx get the value from __port.
* %0 is replaced with eax. (because of "a"). I'm not at all sure about this. Since "a" is only an output operand, will %0 be replaced with eax ?
* The instruction is executed.
* The value from eax is copied to portval.
Is this correct ?
2. Since %0 is always referring to eax, will this implementation be equivalent:
Code: Select all
__asm__ __volatile__ ("in %%eax, %1":"=a"(portval):"Nd"(__port));
4. If I omitted the "N", then is it correct that value of __port is copied to edx and %1 is replaced by edx ?
5. The GCC manual says "N" is
But here __port is unsigned short int which I believe would be 16 bits. So how is it decided which portion of the 16 bits is used in the inline assembly ?Unsigned 8-bit integer constant (for in and out instructions).