Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does OFFSET in 16 bit assembly code mean?

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?

like image 215
Without Me It Just Aweso Avatar asked Nov 03 '09 19:11

Without Me It Just Aweso


People also ask

What does offset mean in assembly?

In assembly language In computer engineering and low-level programming (such as assembly language), an offset usually denotes the number of address locations added to a base address in order to get to a specific absolute address.

What is offset in arm?

The ARM instruction set architecture has three addressing modes: Immediate. The offset is an unsigned integer that is stored as part of the instruction. It can be added to or subtracted from the value in the base register.

What is the use of offset directives?

It is used to load the offset of a variable into a register so that variable can be accessed with one of the addressed modes.

What is the difference between Lea and offset command?

Also offset ar - is immediate value which is calculated during translation. And lea - is actual processor instruction "Load Effective Address" with second operand which references to memmory.


2 Answers

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.

like image 65
Nathan Fellman Avatar answered Dec 28 '22 06:12

Nathan Fellman


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

like image 34
copumpkin Avatar answered Dec 28 '22 07:12

copumpkin