clobbering input args in inline asm

Andrew Haley aph@redhat.com
Sat Apr 29 08:51:00 GMT 2006


Stas Sergeev writes:
 > Hi.
 > 
 > As probably many of the new gcc users, I stumbled over
 > the fact that gcc allows an inline asm to modify the
 > input arguments, and there seem to be no other way
 > of preventing that, than to use the dummy output var.
 > And, just as all the others who stumbled over this, I
 > found that solution counter-intuitive. So I tried some
 > other things, and now I have a few questions.
 > Below is the program that prints the different results
 > when compiled with -O0 and -O2, and the fact that the
 > asm constraints are broken, is really counter-intuitive,
 > IMHO.
 > 
 > My questions are: - I can't add the register both in the input list
 > and the clobber list, and that's perfectly documented. However, it
 > seems like I can add "0" in the clobber list, and gcc accepts that.

The clobber list is for physical register names such as "ebx", not for
operands.  This is for asms that must clobber specific physcial
registers.

----------------------------------------------------------------------
 Some instructions clobber specific hard registers.  To describe this,
write a third colon after the input operands, followed by the names of
the clobbered hard registers (given as strings).  Here is a realistic
example for the VAX:

     asm volatile ("movc3 %0,%1,%2"
                   : /* no outputs */
                   : "g" (from), "g" (to), "g" (count)
                   : "r0", "r1", "r2", "r3", "r4", "r5");
----------------------------------------------------------------------

 >  Though, it still doesn't give the right code. I wasn't able to
 > find in the gcc manual, nor in the google, what will the "0" mean
 > in the clobber list - so what is it?

 > - Another strange thing is that I can add "0" in the clobber list
 > if it refers to any of the register classes (on i386 target), but
 > not to "a". I.e. if I have "b" in input and "0" in clobber - gcc
 > eats that. If I have "a" in input and "0" in clobber - gcc
 > complains. What is the reason?

 > - Why wouldn't gcc just allow the "&" modifier in the input list?
 > Just like in the output list it means early-clobber, in the input
 > list it could mean late-clobber, i.e. clobbered after the value was
 > taken.

 > #include <stdio.h>
 > 
 > volatile int a = 5;
 > 
 > int main()
 > {
 >   int b = a;
 >   asm volatile ("movl $0, %%ebx\n" :: "b"(b));
 >   printf("%i\n", b);
 >   return 0;
 > }

This is wrong: you're clobbering one of the inputs without telling
gcc.  Assuming you actually want the output, it should be:


#include <stdio.h>

volatile int a = 5;

int main()
{
  int b = a;
  asm volatile ("movl $0, %%ebx\n" : "=b"(b));
  printf("%i\n", b);
  return 0;
}


And if you don't want the ouput, add ebx to the clobber list:


volatile int a = 5;

int main()
{
  int b = a;
  asm volatile ("movl $0, %%ebx\n" :::"ebx");
  printf("%i\n", b);
  return 0;
}


Andrew.



More information about the Gcc-help mailing list