Page 1 of 1

Inline Assembly [Linux source code]

Posted: Mon Feb 07, 2011 5:30 am
by osdevkid
Dear all,

I am trying to understand "Inline Assembly", which is used on Linux source code in several places.

Here is one example,

Code: Select all

asm ("cld\n\t"
     "rep\n\t"
     "stosl"
     : /* no output registers */
     : "c" (count), "a" (fill_value), "D" (dest)
     : "%ecx", "%edi" );
The above stores the value in "fill_value" "count" times to the pointer "dest".

From the online tutorials, I understood something about Inline Assembly, but still I can't able to understand about the last parameter

Code: Select all

: "%ecx", "%edi"
The explanations tells, the above is the clobberlist, I can't able to understand this term, my understanding was
"It instructs GCC compiler to use these registers ECX and EDI wihtout worry about its eariler & later usage."

Is my understanding is correct ?

Re: Inline Assembly [Linux source code]

Posted: Mon Feb 07, 2011 6:23 am
by Combuster
Either its phrased wrong or you misinterpreted it, because it is quite the opposite: everything in the clobber list are those registers and resources that are modified, and therefore should not be relied on by the surrounding code. rep stos adjusts ecx and edi on each iteration, so the output values are changed from the input and they are in the clobber list. edx is for example not used, and therefore not in the clobber list. eax is used, and written to, but since GCC does the write (it is listed in the input list), and it does not change during the actual assembly, the value afterwards is exacly what gcc expects it to be, and as a result, eax is not in the clobber list.

The memory operand is however missing from the clobber list - memory is written, and variables stored in memory may be altered without the knowledge of gcc.

Re: Inline Assembly [Linux source code]

Posted: Mon Feb 07, 2011 6:37 am
by Solar
Exact documentation on inline ASM in GCC: From the horse's mouth.

Combuster is correct - the clobber list warns the compiler that the listed registers will change when executing the ASM code. (So the compiler doesn't keep vital data cached in them.)

Re: Inline Assembly [Linux source code]

Posted: Mon Feb 07, 2011 7:02 am
by osdevkid
At first, I would like to thank you for the great response.
eax is used, and written to, but since GCC does the write (it is listed in the input list), and it does not change during the actual assembly, the value afterwards is exacly what gcc expects it to be, and as a result, eax is not in the clobber list.
In case, assume EAX is stored with some numeric values, after my inline assembly code, EAX filled with the value "fill_value", now EAX value modified. So we should add this register in to clobberlist.

Is it correct ? or GCC has an intelligence to store & restore the previous values of registers used in "input list" of inline assembly code.

Please clarify me...

Re: Inline Assembly [Linux source code]

Posted: Mon Feb 07, 2011 7:15 am
by Solar
Have you followed my link?
GCC Manual wrote:You may not write a clobber description in a way that overlaps with an input or output operand. For example, you may not have an operand describing a register class with one member if you mention that register in the clobber list. Variables declared to live in specific registers (see Explicit Reg Vars), and used as asm input or output operands must have no part mentioned in the clobber description. There is no way for you to specify that an input operand is modified without also specifying it as an output operand. Note that if all the output operands you specify are for this purpose (and hence unused), you will then also need to specify volatile for the asm construct, as described below, to prevent GCC from deleting the asm statement as unused.

Re: Inline Assembly [Linux source code]

Posted: Mon Feb 07, 2011 7:29 am
by osdevkid
Hi Solar,

Sorry I missed to notice your link. Thank you very much.