Question

I am trying to write a function with an unknown number of parameters in assembly, and at one point I wish to leal into %esp to fetch a byte at a certain offset on the stack. I am trying to do it this way:

movl        offset,%eax
leal        (%eax,%esp,1),%eax #
movb        %bl,(%eax)

Where offset is a 4 byte .long storing the offset of the most recently used argument in %esp. When assembling this piece of code, however, I get this message:

Error: `(%eax,%esp,1)' is not a valid base/index expression

I am assembling for IA32 in kubuntu using GCC4.8. Gnu syntax, please :)

Thanks in advance!

Was it helpful?

Solution

Indeed it isn't valid, esp can not be used as an index register. The solution is simple though, because it is allowed as a base: just swap the operands and use leal (%esp, %eax), %eax. Alternatively, add %esp, %eax.

PS: movb %bl,(%eax) is a memory write, not a read, and you said you were trying to "fetch" a byte.

PS #2: of course you don't even need to calculate the address, you can do that in the mov instruction: movb (%esp, %eax), %bl

OTHER TIPS

Assuming this is 32 bit code, this isn't going to work because using esp as a base register uses ss (stack segement) instead of ds (data segment), and depending on the OS, ss and ds may not be logically equal. You could use

        movb    (%esp+offset), %bl

I don't know GNU syntax, but since the operand bl is a byte, you may be able to use "mov" instead of "movb" (if this is allowed with GNU syntax).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top