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开发者_StackOverflow社区 a better way?
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
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.
精彩评论