Given is the assembly program of Intel 8086 Processor which adds the numbers in array:
.model small
.stack 100h
.data
array dw 1,2,3,1,2
sum dw ?,", is the sum!$"
.code
main proc
mov ax,@data
mov ds,ax
mov di,0
repeat:
mov ax,[array+di]
add sum,ax
add di,2 ; Increment di with 2 since array is of 2 bytes
cmp di,9
jbe repeat ; jump if di<=9
add sum,30h ; Convert to ASCII
mov ah,09h
mov dx,offset sum ; Printing sum
int 21h
mov ax,4c00h
int 21h
main endp
end main
Above program adds the number of array using "base + index" addressing mode.
The same operation can by performed by something like:
mov ax, array[di]
Now I have following questions here:
array[di]
and [array+di]
array[di]
?According to the book The Art of Assembly Language, array[di]
and [array + di]
are both "indexed addresing modes", so, none is better than the other, is just different syntax for the same thing. The section 4.6.2.3 Indexed Addressing Modes of the book explains that the important thing is the presence of a constant value and an index (or base) register :
The indexed addressing modes use the following syntax:
mov al, disp[bx] mov al, disp[bp] mov al, disp[si] mov al, disp[di]
The offsets generated by these addressing modes are the sum of the constant and the specified register.
You may substitute si or di in the figure above to obtain the [si+disp] and [di+disp] addressing modes.
We are calling "constant value" to the variable array
because variables are offsets in data segment (so they are constant values), as explained here :
Variable is a memory location. For a programmer it is much easier to have some value be kept in a variable named "var1" than at the address 5A73:235B.
It is important to mention that different assemblers may use different syntax for the same addressing modes, for example, MASM vs NASM, or NASM vs GAS.
There are other addressing modes that change the size (in bytes) and performance (in clock cycles) of the instructions involved, as can be read here. Next is instruction MOV
and the addressing modes :
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