汇编中 array[di] 和 [array + di] 寻址的根本区别是什么?

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

给出的是Intel 8086处理器的汇编程序,它将数组中的数字相加:

.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

以上程序使用"base + index"寻址方式添加数组个数。

相同的操作可以通过以下方式执行:

mov ax, array[di]

现在我有以下问题:

  1. array[di][array+di]有什么区别
  2. 哪种内存寻址方式array[di]?
  3. 哪个更好用,为什么?

根据书本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的解释,重要的是常量值和索引(或基数)寄存器的存在

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.

我们正在调用 "constant value" 变量 array 因为变量是数据段中的偏移量(因此它们是常量值),如 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.

值得一提的是,不同的汇编器可能对相同的寻址模式使用不同的语法,例如,MASM vs NASM, or NASM vs GAS.

还有其他寻址模式可以改变所涉及指令的大小(以字节为单位)和性能(以时钟周期为单位),可以阅读here。接下来是指令 MOV 和寻址模式: