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?
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.
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.
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.
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.
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
.
It just means the address of that symbol. It's a bit like the & operator in C, if you are familiar with that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With