How to set a variable in GCC with Intel syntax inline assembly?

user541686 picture user541686 · Mar 22, 2011 · Viewed 24.4k times · Source

Why doesn't this code set temp to 1? How do I actually do that?

int temp;
__asm__(
    ".intel_syntax;"
    "mov %0, eax;"
    "mov eax, %1;"
    ".att_syntax;"
    : : "r"(1), "r"(temp) : "eax");
printf("%d\n", temp);

Answer

Carl Norum picture Carl Norum · Mar 22, 2011

You want temp to be an output, not an input, I think. Try:

  __asm__(
      ".intel_syntax;"
      "mov eax, %1;"
      "mov %0, eax;"
      ".att_syntax;"
      : "=r"(temp)
      : "r"(1) 
      : "eax");