rares.urdea
rares.urdea

Reputation: 660

8086 assembly, about the PTR operator

this might be simple and silly but i'll ask it anyway since i don't seem to be able to figure it out.

a simple code snippet:

 assume cs:code, ds:data
 data segment
    b dd 256
 data ends

 code segment
 start:
        mov ax,data
        mov ds,ax
        mov ax,word ptr b     -> the result of this operation will be ax:= 256 (100h)

        mov bx,word ptr b+1   -> while this is going to be bx:= 1

I suppose it has something to do with the address and it being moved by a byte or something but i'm not exactly sure, and i really need to understand.

Thanks in advance !

Upvotes: 2

Views: 12739

Answers (2)

Dirk Wolfgang Glomp
Dirk Wolfgang Glomp

Reputation: 539

We need to specify the number of bytes only if we want to write an immediate value to a ram location. Otherwise our assembler use the registersize for to determine how many bytes we want to access.

Except for using a dword access with using MASM, then we need also the declaration of DWORD PTR, if we want a dword access, example writing the content of EAX to a ram location, or reading a dword from a ram location into EAX. Same with all other 32 Bit-Register.

I think it is better to declare a named ram location with the define bytes(DB) notation, if we want a multiple access with a byte, word, and/or dword access.

b DB 0, 1, 0, 0

The registersize is well known and we do not need to specify:

mov al, [b]
mov [b], al

mov bl, [b+1]
mov [b+1], bl

mov ax, [b]
mov [b], ax

mov bx, [b+1]
mov [b+1], bx

Except for MASM:

mov eax, dword ptr[b]
mov dword ptr[b], eax

Using imediate values:

mov byte ptr[b], 0
mov word ptr[b], 256
mov dword ptr[b], 256

Dirk

Upvotes: 0

sharptooth
sharptooth

Reputation: 170489

This

mov ax,word ptr b

is easier to understand when formatted like this:

mov ax, word ptr [b]

which has effect of copying a word at address b into ax. So this

mov bx,word ptr b+1 

is in fact

mov bx,word ptr [b+1]

which has effect of copying a word at address b+1 into bx.

Now dd instructs to allocate a dword and assign 256 to it. 256 in hex is represented as 00000100h. The dword will be allocated in memory like this 00010000h - bytes 00h, 01h, 00h, 00h because on x86 lower bytes are stored at lower addresses (so-called little endian).

When memory is being read into registers bytes order is changed such that lower bytes occupy lower bits in the register ("natural", so-called big endian). So 00h, 01h in memory is turned into 0100h inside a register and 01h, 00h in memory is turned into 0001h inside register.

So the first mov gets the first two bytes from b (00h, 01h) and makes ax==0100h and the second one gets the middle two bytes from b (01h, 00h) and makes bx==0001h.

Upvotes: 11

Related Questions