Richard
Richard

Reputation: 15592

square brackets in inline assembly

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).

Upvotes: 1

Views: 1307

Answers (1)

Ross Ridge
Ross Ridge

Reputation: 39621

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.

Upvotes: 3

Related Questions