1

I am rather confused by the concept OFFSET Operator. According to Kip R. Irvine's book Assembly Language for x86 Processors, he defines the Offset Operator as the operator that returns the distance of a variable from the beginning of its enclosing segment. He also says the Offset Operator returns the offset of a data label and that represents the distance (in bytes) of the label from the beginning of the data segment. What is the offset? What does he mean by the distance of the label from the beginning of the data segment? Also, did he come about to this result:

He declares three different types of variables:

.data
bVal  BYTE ?
wVal  WORD ?
dVal  DWORD ?
dVal2 DWORD ?

If bVal were located at offset 00404000 (hexadecimal), the OFFSET operator would return the following values:

mov esi, OFFSET bVal     ;ESI = 00404000h
mov esi, OFFSET wVal     ;ESI = 00404001h
mov esi, OFFSET dVal     ;ESI = 00404003h
mov esi, OFFSET dVal2    ;ESI = 00404007h

Where did he arrive at those values? Please help. Thank you so much!

gordon sung
  • 605
  • 2
  • 8
  • 27
  • 5
    Look at how many bytes make up each of the variables bVal, wVal, dVal, dVal2 and imagine they are all placed in memory one after the other. And then look at each of the values for `ESI =` . Offset pretty much says "give me the address of the label". Consider the offset 00404000h as arbitrary and used for illustrative purposes. – Michael Petch Sep 14 '16 at 04:24

2 Answers2

6

Outside of 16-bit code, on normal OSes, virtual memory is flat, with all the segments having base=0.

So it's just a complicated way to say that OFFSET var gives you the address of var as an immediate, instead of loading from it.

mov esi, bVal          ; load from [bVal], in MASM syntax

mov esi, OFFSET bVal   ; esi= address of bVal
mov esi, [esi]         ; load from [bVal]

See also Assembly difference between [var], and var for the difference between MASM and NASM syntax.

Community
  • 1
  • 1
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
1

The theory of offset means,"An offset is the number of address location in based address in order to go to the specifice absolute address.".So,it look like an index (a data item or a field) of an array (a data element or a block or a frame).An offset indicate the distance between data item and data element.All items of an element are the same size (typically given in bytes or words).

So,in your case,".data" is a memory segment or a block or an element and variables in this segment are data items or offsets.This offsets are virtual address of the space of the non-contiguous areas of physical memory.So,this numbers represent the virtual address of the space of the non-contiguous areas of physical memory.