Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 556
jian2587 Avatar asked Dec 13 '09 19:12

jian2587


People also ask

What is Q in assembly language?

w = word (16 bit). l = long (32 bit integer or 64-bit floating point). q = quad (64 bit). t = ten bytes (80-bit floating point).

How do registers work in x86?

The x86 processor maintains an instruction pointer (EIP) register that is a 32-bit value indicating the location in memory where the current instruction starts. Normally, it increments to point to the next instruction in memory begins after execution an instruction.


1 Answers

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.

like image 157
DigitalRoss Avatar answered Oct 18 '22 07:10

DigitalRoss