Inline Assembler: Which zero registers can be used?

When pasting inline assembler into a function in a C-like language, what is the convention about which registers you can use for scratch? Does the compiler have to save the values ​​of all registers that must be saved before entering the asm block? Does the programmer have to store the values ​​in these registers somewhere and restore them before exiting the asm block? Is there a typical convention or is it implementation specific?

+6
c assembly inline-assembly conventions low-level
source share
3 answers

Embedded assembly is, by definition, specific to the compiler.

Most compilers that support embedded assembly have syntax that allows you to specify which registers are modified by the assembly. The compiler can then save and restore these registers as needed.

+9
source share

This is very compiler specific. However, for a realistic example, take gcc on x86. Format:

 asm ( assembler template : output operands (optional) : input operands (optional) : list of clobbered registers (optional) ); 

Where is the "written-off register list" you say that the compiler that registers your code uses.

Here is a simple memory copy code:

 asm ("movl $count, %%ecx; up: lodsl; stosl; loop up;" : /* no output */ :"S"(src), "D"(dst) /* input */ :"%ecx", "%eax" ); /* clobbered list */ 

Given these guidelines, gcc will not use eax and ecx for other things in the block.

More details here .

+7
source share

You can read about register usage in some calling conventions here .

+2
source share

All Articles