Linux Kernel Architecture

(Jacob Rumans) #1
Mauerer app03.tex V1 - 09/04/2008 6:11pm Page 1195

Appendix C: Notes on C


: Modified registers
);

On IA-32 systems, the assembler code itself must be given in AT&T notation (on all other platforms,
the preferred notation of the particular architecture is used). Input and output register specifications
establish which input parameters are supplied in registers (or in memory) and which registers or memory
positions are used to output values. To be more accurate, they define the various conditions for the
registers involved and therefore represent the interface to the C implementation that supplies the input
data and further processes the output data. By specifying allmodified registers that are changed in the
assembler statements (although they are not part of the input and output specification), the compiler is
provided with additional information. For example, prior to execution of the assembler code, modified
registers may not be used by the compiler to store values that it needs to access later. Notice that the
original GCC documentation refers to ‘‘modified registers‘‘ asclobbered registers.


For the purposes of this appendix, it is sufficient to summarize the AT&T assembler syntax into the
following five rules:


❑ Registers are referenced by prefixing their name with a percent symbol. For example, to use reg-
istereax,%eaxmust appear in the assembler code.

Twopercent symbols must be specified inC source code in order to generateone
percent symbol in output that is forwarded to the assembler program.

❑ The source register is always specified before the destination register. For example, inmovstate-
ments, this means thatmov a, bcopies the contents of registerainto registerb.
❑ The operand size is given by a suffix after the assembler statement.bstands forbyte,lforlong,
andwforword. To move a long value from registereaxto registerebxonIA-32systems,itis
therefore necessary to specifymovl %eax, %ebx.
❑ Indirect memory references (de-referencing of pointers) are possible by including a register in
parentheses. For example,movl (%eax), %ebxmoves the long value at the address in memory
pointed to by the value of registereaxto the registerebx.
❑ offset(register)specifies that the register value is to be used together with an offset that is
added to its actual value. For example,8(%eax)specifies thateax + 8is to be used as an operand.
This notation is used primarily for memory access — for example, to specify offsets from the
stack or frame pointer in order to access certain local variables.

The following example illustrates the meaning of the input and output specifications:


int move() {
int a = 5;
int b;

asm ("movl %1, %%eax;
movl %%eax, %0;"
: "=r"(b) /* Output register */
: "r" (a) /* Input register */
: "%eax"); /* Modified registers */

printf("b: %u\n", b);
}
Free download pdf