What does OFFSET in 16 bit assembly code mean?

Without Me It Just Aweso picture Without Me It Just Aweso · Nov 3, 2009 · Viewed 82.8k times · Source

I am going through some example assembly code for 16-bit real mode.

I've come across the lines:

    mov    bx, cs
    mov    ds, bx
    mov    si, OFFSET value1
    pop    es
    mov    di, OFFSET value2

what is this doing? What does having 'OFFSET' there do?

Answer

Nathan Fellman picture Nathan Fellman · Nov 3, 2009

As some of the other answers say, the offset keyword refers to the offset from the segment in which it is defined. Note, however, that segments may overlap and the offset in one segment may be different in another segment. For instance, suppose you have the following segment in real mode

data SEGMENT USE16 ;# at segment 0200h, linear address 2000h

    org 0100h
    foo db 0

    org 01100h
    bar db 0

data ENDS

The assembler sees that foo is at offset 0100h from the base of data SEGMENT, so wherever it sees offset foo it will put the value 0100h, regardless of the value of DS at the time.

For example, if we change DS to something other than the base of the data segment the assembler is assuming:

mov ax, 200h            ; in some assemblers you can use @data for the seg base
mov ds, ax

mov bx, offset foo          ; bx = 0100h
mov byte ptr [bx], 10       ; foo = 10


mov ax, 300h
mov ds, ax

mov bx, offset foo          ; bx = 0100h
mov byte ptr [bx], 10       ; bar = 10, not foo, because DS doesn't match what we told the assembler

In the second example DS is 0300h, so the base of the segment pointed to by DS is 03000h. This means that ds:[offset foo] points to the address 03000h + 0100h which is the same as 02000h + 01100h, which points to bar.