Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dereferencing a label in x86 assembly

Tags:

x86

assembly

Consider this x86 assembly code:

section .data

foo:
    mov ebx, [boo]
    mov [goo], ebx
goo:
    mov eax, 2
    mov eax, 3
    ret
boo:
    mov eax, 4
    mov eax, 5
    ret

What exactly is going on here? When I dereference [boo] and mov it to [goo] what exactly am I moving there? Just one command? The ret as well?


Follow-up questions:

  1. Does dereferencing a label give me an address? Or the machine code for the first command in the label?
  2. If it's a machine code - how can it possibly be more than one command? Aren't all commands essentially 32-bit (even if not all bits are used)?
  3. Bottom line - will eax have a value of 3 or 5 at the end?
like image 288
Yuval Adam Avatar asked Aug 18 '09 21:08

Yuval Adam


1 Answers

boo is the offset of the instruction mov eax, 3 inside section .data. mov ebx, [boo] means “fetch four bytes at the offset indicated by boo inside ebx”. Likewise, mov [goo], ebx would move the content of ebx at the offset indicated by goo.

However, code is often read-only, so it wouldn't be surprising to see the code just crashing.

Here is how the instructions at boo are encoded:

boo:
b8 03 00 00 00          mov    eax,0x3
c3                      ret

So what you get in ebx is actually 4/5 of the mov eax, 3 instruction.

like image 182
Bastien Léonard Avatar answered Sep 30 '22 15:09

Bastien Léonard