clobbering input args in inline asm

Stas Sergeev
Fri Apr 28 22:35:00 GMT 2006


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,

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. 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.

Sorry for the novice questions. This is mainly just my curiosity
and the fact that I find an official solution very confusing and

Thanks for any info.

The test-case:
#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;
Prints 5 when compiled with -O0, and 0 when compiled with -O1.

More information about the Gcc-help mailing list