5

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?
zx485
  • 28,498
  • 28
  • 50
  • 59
HQuser
  • 640
  • 10
  • 26
  • 6
    There is no difference, but the standard syntax is `[array+di]`. Some assemblers also accept the other version. – Jester Nov 17 '16 at 15:56
  • "3." is heavily opinion based, as your assembler dictates what you can use... I would go with `[array+di]` because A) IIRC that's the official way how Intel syntax was defined by Intel B) it's compatible with NASM - which looks to be one of ultimate assemblers for x86, ie. maintained, multi platform, multi target and open source. And even cost-free. Hard to beat that combo. C) makes more sense to me, I'm used to think rather in pointers than in "variables", concept of variables was shown to me after I already did know Z80 assembler. – Ped7g Nov 18 '16 at 15:49

1 Answers1

6

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

  • 1
    Are those cycle counts for 8086? I doubt that's relevant, unless emu8086 is actually a cycle-accurate simulator (which I highly doubt). – Peter Cordes Nov 18 '16 at 07:43
  • 3
    Anyway, it's not just that they're both "indexed addressing modes", they're literally the same addressing mode. It's just a different asm syntax for the same machine code. Oh, I missed part2 of the question, yeah I guess all this stuff explaining that addressing mode is relevant. – Peter Cordes Nov 18 '16 at 07:44