ladookie
ladookie

Reputation: 1371

What does equals sign g "=g" in GCC inline assembly mean / do?

I'm not sure what this inline assembly does:

asm ("mov %%esp, %0" : "=g" (esp));

especially the : "=g" (esp) part.

Upvotes: 8

Views: 3107

Answers (2)

Nemo
Nemo

Reputation: 71525

If you want the gory details, read the GCC documentation on Extended Asm.

The short answer is that this moves the x86 stack pointer (%esp register) into the C variable named "esp". The "=g" tells the compiler what sorts of operands it can substitute for the %0 in the assembly code. (In this case, it is a "general operand", which means pretty much any register or memory reference is allowed.)

Upvotes: 9

ughoavgfhw
ughoavgfhw

Reputation: 39905

"=g" (esp) defines an output for the inline assembly. The g tells the compiler that it can use any general register, or memory, to store the result. The (esp) means that the result will be stored in the c variable named esp. mov %%esp, %0 is the assembly command, which simply moves the stack pointer into the 0th operand (the output). Therefore, this assembly simply stores the stack pointer in the variable named esp.

Upvotes: 9

Related Questions