0

My question is about OFFSET in assembly language

I have written code below :(emu8086)

data segment
DATA1  dw 6
DATA2 dw 5
ends

stack segment
dw   128  dup(0)
ends

code segment
start:

mov ax, data
mov ds, ax
mov es, ax



mov bx, [offset DATA1]
mov ax, [offset DATA2]


mov ax, 4c00h ; exit to operating system.
int 21h    
ends

end start ; set entry point and stop the assembler.

I excepted when I type [offset DATA1] I receive 6 and when I type [offset DATA2] I receive 5.

My reason was this : offset gives us the address of a data and this sign[] give us the content of the mentioned place of memory. so when we use [offset data1] we should receive data1 (here is 6)

But the result is some thing else. after running this code ax is 0006H and bx is 0000H and I can't understand why.

can anyone please help me to understand the meaning of [offset someData]?

I know It is very basic but I'm new in assembly.

Thanks in advance for your attention

1 Answer 1

0

I tried to assemble your code with TASM and received BX=0 and AX=2 as expected:

cs:0000 B88F4E         mov    ax,4E8F
cs:0003 8ED8           mov    ds,ax
cs:0005 8EC0           mov    es,ax
cs:0007 BB0000         mov    bx,0000
cs:000A B80200         mov    ax,0002
cs:000D B8004C         mov    ax,4C00
cs:0010 CD21           int    21

In most assemblers, MOV reg,variable will load the reg with offset of variable and MOV reg,[variable] with the variable contens. This was not the case of MASM, which interprets MOV reg,variable as loading reg with memory-variable contents and you had to explicitely write MOV reg,OFFSET variable to load the offset part of variable address. Your syntax is mixed up and it is the business of used assembler whether it will parse MOV reg,[offset variable] as offset or as contents or generate some warning. Omitt the word offset if you want to load the contents of DATAx. You may also have to specify ASSUME DS:data to tell your assembler that you have loaded DS register with segment address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.