1

I try to use lea rax, [rip] in a c program. My program is following:

...
asm volatile ("lea %%rax, %[rip]"
             :);
...

However, the program does not compile, throwing an error: undefined name operand. My platform is Ubuntu 1404 on a x86-64 architecture (as virtual machine).

Richard
  • 14,642
  • 18
  • 56
  • 77
  • 1
    are you sure about the syntax and order of destination/source operands? See here if you use gcc: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html – Pynchia Jun 07 '15 at 20:15
  • could you be more specific (considering a non-expert here)? I have updated the OP a bit, and regarding `lea` instruction, am following here: http://stackoverflow.com/questions/12397451/why-cant-i-save-the-value-of-rip . – Richard Jun 07 '15 at 20:30
  • I think in GCC's inline assembly, [] or %[] denotes a named parameter that you pass in. rip is actually the name of a machine register, not the name of one of your parameters so it makes no sense to refer to it that way. Did you mean for it to be an address operand? – sh1 Jun 07 '15 at 20:37

1 Answers1

3

In order to use lea rax, [rip] in inline assembly with GCC you need to convert it to AT&T syntax and the quote it correctly so the % characters aren't interpreted as operand substitutions. So for example:

asm volatile ("lea (%%rip),%%rax"
          ::: "rax");

Note that since this doesn't actually do anything useful, you have no access to the value stored in RAX in your C code, you should specify an output operand:

long long ip;
asm ("lea (%%rip),%0"
     : "=r" (ip));

This will let you access the value through the variable ip.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
  • excellent answer. @Richard: in case you want to use Intel's syntax, please read here: http://stackoverflow.com/questions/5397677/gcc-intel-syntax-inline-assembly – Pynchia Jun 07 '15 at 21:08