Inline Assembly [Linux source code]

Programming, for all ages and all languages.
Post Reply
User avatar
osdevkid
Member
Member
Posts: 72
Joined: Sun Nov 21, 2010 11:15 am
Location: India, Chennai

Inline Assembly [Linux source code]

Post 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 ?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Inline Assembly [Linux source code]

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Inline Assembly [Linux source code]

Post 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.)
Every good solution is obvious once you've found it.
User avatar
osdevkid
Member
Member
Posts: 72
Joined: Sun Nov 21, 2010 11:15 am
Location: India, Chennai

Re: Inline Assembly [Linux source code]

Post 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...
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Inline Assembly [Linux source code]

Post 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.
Every good solution is obvious once you've found it.
User avatar
osdevkid
Member
Member
Posts: 72
Joined: Sun Nov 21, 2010 11:15 am
Location: India, Chennai

Re: Inline Assembly [Linux source code]

Post by osdevkid »

Hi Solar,

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