gordon sung
gordon sung

Reputation: 605

OFFSET Operator in Assembly language for x86 Processors

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!

Upvotes: 1

Views: 12230

Answers (2)

Peter Cordes
Peter Cordes

Reputation: 363980

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.

Upvotes: 7

Min Thuta Shein
Min Thuta Shein

Reputation: 33

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.

Upvotes: 1

Related Questions