Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the fundamental difference between addressing of array[di] and [array + di] in assembly?

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:

  1. What's the difference between array[di] and [array+di]
  2. Which memory addressing mode is array[di]?
  3. Which one is better to use and why?
like image 421
HQuser Avatar asked Nov 17 '16 15:11

HQuser


1 Answers

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.

enter image description here

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 :

enter image description here enter image description here

like image 171
Jose Manuel Abarca Rodríguez Avatar answered Oct 19 '22 20:10

Jose Manuel Abarca Rodríguez