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?
Asked
Active
Viewed 2,790 times
2 Answers
1
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

Abyx
- 12,345
- 5
- 44
- 76
0
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.

GJ.
- 10,810
- 2
- 45
- 62
-
I'm not using an OS. I'm in protected mode, so there is a segment descriptor, not an actual segment number. And in my case there are only two segment descriptors, for data and code (not counting the null one). – Neo_b Sep 13 '10 at 20:34