Page 1 of 1

Assembly troubles

Posted: Wed May 19, 2010 9:43 am
by davidv1992
I am currently writing a compiler produces code somewhat like the following example (simplified for testing):

Code: Select all

.intel_syntax noprefix
.global tigermain
.section .text
tigermain:
enter 0,0

mov ebx, L0
jmp ebx
L0:

leave
ret
however, the jump doesn't work.
I assemble it with gas, and then link it to a runtime written in c which calls tigermain. when i study the code with objdump the move seams to be correct, however, when debuging with gdb the load loads junk into ebx, instead of the label's adress. can anyone explain where it is going wrong?

Re: Assembly troubles

Posted: Wed May 19, 2010 11:10 am
by Combuster
It works as expected here.

Are you looking at ld's default address 0x08048xxx because you didn't set a offset for .text yourself?

Re: Assembly troubles

Posted: Wed May 19, 2010 11:26 am
by davidv1992
in objdump I see the default values, it is linked to another file and glibc (called with gcc (was easier)).

What I do suspect now though is that somehow it is loading the content at the label, instead of it's adress, and im currently doing some experiments to verify.

Which leads to another question, how the h*ck do I persuade it to load the address?

It is probably also worth saying that i'm working on a 64 bit machine, and am contiually passing --32 or -m32 to gas/gcc

Update: hunch was right, both opcode and the value that gdb returns confirm this. Can anyone tell me how I should do this?

Re: Assembly troubles

Posted: Wed May 19, 2010 11:51 am
by tharkun
This is a bug with GAS's intel_syntax feature.
E.g. Using GAS, this will load the contents of [L0]

Code: Select all

mov ebx, L0
Whereas this will load the address of L0:

Code: Select all

lea ebx, [L0]

Re: Assembly troubles

Posted: Wed May 19, 2010 12:24 pm
by davidv1992
thanks for the help, probably wouldn't have figured that out myself.