Neo_b
Neo_b

Reputation: 241

Addressing data using relative pointers (x86-32 assembler)

I'm writing in 32-bit x86 assembler, and I'm not quite sure how to address data that is always in the same relation to the code. Do I have to use EIP to calculate the absolute address, or is there a better way?

Upvotes: 1

Views: 2938

Answers (2)

Abyx
Abyx

Reputation: 12928

You can use position-independent code:

   call @f
   dd 42 ; data
@@:
   pop eax ; eax contains offset of data
   mov eax, cs:[eax]

or use the same with delta-offsets

   call base
base:
   pop ebp
   sub ebp, base ; to use small offsets, -128 to +127, and smaller instruction size
   ;....
   mov eax, cs:[ebp+dataN-base] ; dataN-base is called "delta-offset"
   ;....
data1:
   dd 100
   ;....
dataN:
   dd 200

Upvotes: 1

GJ.
GJ.

Reputation: 10937

Depend of OS. Normally have segment registers DS (data segment) and CS (code segment) different values. So you can use cs prefix like:

mov    edx, cs:[eax]

In that case the default prefix is ds segment register.

Upvotes: 0

Related Questions