What does OFFSET in 16 bit assembly code mean?

Viewed 102492

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?

6 Answers

From MASM Programmer's Guide 6.1 (Microsoft Macro Assembler)

The OFFSET Operator

An address constant is a special type of immediate operand that consists of an offset or segment value. The OFFSET operator returns the offset of a memory location, as shown here:

    mov     bx, OFFSET var  ; Load offset address

For information on differences between MASM 5.1 behavior and MASM 6.1 behavior related to OFFSET, see Appendix A.

Since data in different modules may belong to a single segment, the assembler cannot know for each module the true offsets within a segment. Thus, the offset for var, although an immediate value, is not determined until link time.

If you read carefully, the final value is determined after you "link" your object code to create a DLL/EXE. Prior to linking, all you have is an immediate value which represents the offset from the segment's base address.

Offset is basically the distance from the segment point(also called datum point). for example segment address is 0000 and the offset or logical address is 0100 then the physical address can be counted by adding the two pairs. Physical Address = 0000+0100=0100 Means that our required location is on the address of 0100. Similarly if segment address is 1DDD and offset is 0100 then : Physical address is : 1DDD+0100=1EDD

Means that our destination is 1EDD.

Related