I'm trying out the examples of inline assembly in: http://www.delorie.com/djgpp/doc/brennan/brennan_att_inline_djgpp.html But something is confusing me about clobbering:
- About behavior of clobber
Clobbering essentially tells GCC to not trust the values in the specified register/memories."Well, it really helps when optimizing, when GCC can know exactly what you're doing with the registers before and after....It's even smart enough to know that if you tell it to put (x+1) in a register, then if you don't clobber it, and later C code refers to (x+1), and it was able to keep that register free, it will reuse the computation. Whew."
There's some inconsistency in the tutorial about the clobber list:
For registers specified in input/output list, there's no need to put them in clobber list as GCC knows; However in the example about rep_movsl (or rep_stosl):asm ("cld\n\t" "rep\n\t" "stosl" : /* no output registers */ : "c" (count), "a" (fill_value), "D" (dest) : "%ecx", "%edi" );
although "S, D, c" are in the output operands, they are listed as clobbered again. I tried a simple snippet in C:
#include<stdio.h>
int main()
{
int a[] = {2, 4, 6};
int b[3];
int n = 3;
int v = 12;
asm ("cld\n\t"
"rep\n\t"
"movsl"
:
: "S" (a), "D" (b), "c" (n)
: );
// : "%ecx", "%esi", "%edi" );
printf("%d\n", b[1]);
}
If I use the commented clobber list, GCC will complain:
a.c:8:3: error: can't find a register in class ‘CREG’ while reloading ‘asm’ a.c:8:3: error: ‘asm’ operand has impossible constraints
If I use empty clobber list, it will compile and the output is 4.