Steve Dell
Steve Dell

Reputation: 605

Inline asm in GCC, specifying offset by expression

Not exactly sure how to title this, but I want to emulate this code:

asm("movl %%fs:0x30, %0" : "=r" (peb) : : );

but I want to specify the offset variably in C

trying:

    int mee = 48;
asm("movl %%fs:%1, %0"

     : "=r" (peb)
     : "r" (mee)
     :
     );

Error is bad memory operand '%eax'

Upvotes: 1

Views: 1612

Answers (1)

e0k
e0k

Reputation: 7161

For what you have written, the compiler translates the first operand to %fs:%eax because it chooses %eax to be the register holding the value of mee. The addressing mode I think you're trying to use is base:offset and the offset must be a value rather than a register. This is the reason for the error "bad memory operand". It worked in the %fs:0x30 case because 0x30 is an immediate value.

To use the register %eax as an offset, try a dereference of it %%fs:(%1) to get the value in the register:

       int mee = 48;
       asm("movl %%fs:(%1), %0" : "=r" (peb) : "r" (mee) :);

See also this guide, which contains some possibly useful examples of memory access (and more) in inline assembly.

Upvotes: 2

Related Questions