Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c inline assembly loading a register value to esp register

In a code, I have following declaration

#if GCC == 1
#define SET_STACK(s)    asm("movl temp,%esp");
...
#endif

In the code, exactly at one place, this macro is used, on which line compiler indicates of undefined reference to 'temp'.

temp = (int*)some_pointer;
SET_STACK(temp);

the temp variable is declared as global volatile void pointer

volatile void* temp;

Is there any syntax problem with the inline assembly ? As of my understanding, the inline assembly tries to load the value of temp (not the dereferenced value , but the pointer itself)

like image 405
nmxprime Avatar asked Jul 07 '15 15:07

nmxprime


2 Answers

You have to use extended assembler to pass C operands to the assembler: Read the manual. (Note: as you did not specify which version you are using, I just picked one).

Do not forget to add registers used in the assembler into the clobber list. You should also make the assembler asm volatile.

Depending on your execution environment, it might be a very bad idea to manually manipulate the stack pointer! At least you should put that into a __attribute__((naked)) function, not a macro. The trailing ; in the macro is definitively wrong, you will have that already right after the macro (2 semicolons might break conditional statements!

like image 181
too honest for this site Avatar answered Oct 30 '22 09:10

too honest for this site


If you want to use C variables in GCC inline assembly, you have to make use of the Extended ASM syntax, e.g.:

volatile int temp = 0;
asm("movl %0,%%esp"
  : /* No outputs. */
  : "rm" (temp)
);
like image 34
mastov Avatar answered Oct 30 '22 09:10

mastov