Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 944
user541686 Avatar asked Mar 22 '11 20:03

user541686


2 Answers

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");
like image 182
Carl Norum Avatar answered Oct 06 '22 22:10

Carl Norum


This code does what you are trying to achieve. I hope this helps you:

#include <stdio.h>

int main(void)
{
    /* Compile with C99 */
    int temp=0;

    asm
    (   ".intel_syntax;"
        "mov %0, 1;"
        ".att_syntax;"
        : "=r"(temp)
        :                   /* no input*/
    );
    printf("temp=%d\n", temp);
}
like image 44
DrBeco Avatar answered Oct 06 '22 21:10

DrBeco