views:

28

answers:

2

Hello. 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?

A: 

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
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
+1  A: 

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