views:

395

answers:

4

I am going through some example assembly code for 16bit 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?

Sorry if its a very basic question, i'm new at this.

+1  A: 

It just means the address of that symbol. It's a bit like the & operator in C, if you are familiar with that.

pumpkin
A: 

In x86 16bit mode, address space is not flat; instead, addresses are composed of an offset and a "segment". The "segment" points to a 64K space, offset is within that space.

See http://en.wikipedia.org/wiki/Memory_segmentation

ammoQ
+1  A: 

OFFSET means that si register will be equal to the offset of the variable value1 (not to its actual value). Offset is the address from the beginning of memory segment where the variable is stored. The offset is usually relative to ds segment (in your case ds and cs registers are pointing to the same segment).

Alexey Kalmykov
+3  A: 

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 02000h

    org 0100h
    foo db 0

    org 01100h
    bar db 0

data ENDS

And look at the following code:

mov ax, 0200h
mov ds, ax

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

mov ax, 0300h
mov ds, ax

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

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.

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.

Nathan Fellman