gnu assembler: get address of label/variable [INTEL SYNTAX]
Asked Answered
F

1

7

I have a code like this:

.bss
woof: .long 0

.text
bleh:
...some op codes here.

now I would like to move the address of woof into eax. What's the intel syntax code here for doing that? The same goes with moving bleh's address into, say, ebx.

Your help is much appreciated!

Freddafreddi answered 13/12, 2009 at 19:12 Comment(0)
V
10

The bss section can't have any actual objects in it. Some assemblers may still allow you to switch to the .bss section, but all you can do there is say something like: x: . = . + 4.

In most assemblers these days and specifically in gnu for intel, there is no longer a .bss directive, so you temporarily switch to bss and create the bss symbol in one shot with something like: .comm sym,size,alignment. This is why you are presumably getting an error ".bss directive not recognized" or something like that.

And then you can get the address with either:

lea woof, %eax

or

movl $woof, %eax

Update: aha, intel syntax, not intel architecture. OK:

.intel_syntax noprefix
    lea    esi,fun
    lea    esi,[fun]
    mov     eax,OFFSET FLAT:fun
.att_syntax
    lea     fun, %eax
    mov     $fun, %eax
.data
fun: .long 0x123

All the lea forms should generate the same code.

Vacancy answered 13/12, 2009 at 19:48 Comment(4)
thanks, very enlightening. though, it's not really bss or data section that is my concern here. I just need to know how to load address of a label into an address using gnu INTEL syntax, not AT&T's. I tried mov eax, woof and mov eax, dword ptr [woof], but both gives me the same result. I want to do something like movl $woof, %eax, but using gnu intel syntax.Freddafreddi
thanks DigitalRoss! I was using lea too, but I'm wondering if I can do it using a mov. I presume OFFSET FLAT is the key here?Freddafreddi
Right, there are multiple addressing modes; MOV can load an immediate, which might happen to also be an address, or it can load the contents of an address. Normally on Unix that's movl $woof,%eax vs movl woof,%eax, but in Intel syntax it's a bit more corny. Anyway, "yes", lea and mov can both load an address.Vacancy
For the record, always use mov $foo, reg or mov reg, OFFSET foo, not LEA in 32-bit mode. LEA is 1 byte longer and can run on fewer execution ports on most CPUs, so it's totally pointless for putting a 32-bit constant into a register, including the absolute address of a symbol. (RIP-relative LEA is useful in 64-bit code, but that machine-code feature only exists in 64-bit mode, and it has special syntax in asm source. How to load address of function or label into register)Shillelagh

© 2022 - 2024 — McMap. All rights reserved.