What is the fundamental difference between addressing the array [di] and [array + di] in an assembly?

This is the assembly of the Intel 8086 Processor program, which adds numbers to the 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 the program adds the number of arrays using the addressing mode "base + index".

The same operation can be performed, for example:

 mov ax, array[di] 

Now I have the following questions:

  • What is the difference between array[di] and [array+di]
  • What is the memory addressing mode of array[di] ?
  • Which one is better to use and why?
+5
source share
1 answer

According to the book The Art of Assembly Language , array[di] and [array + di] are both “indexed add modes,” so there is no better than the other, it's just a different syntax for the same thing. Section 4.6.2.3. Indexed addressing modes in the book explain that having a constant value and an index (or base) is important :

The following syntax is used in indexed addressing modes:

  mov al, disp[bx] mov al, disp[bp] mov al, disp[si] mov al, disp[di] 

The shift created by these addressing modes is the sum of the constant and the specified register.

enter image description here

You can replace si or di in the figure above to get the addressing modes [si + disp] and [di + disp].

We call the "constant value" of the array variable, because the variables are offsets in the data segment (therefore they are constant values), as explained here :

A variable is a memory location. It is much easier for a programmer to have some value in a variable named "var1" than at 5A73 :. 235B

It is important to note that different assemblers can use different syntaxes 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 corresponding commands, as can be read here . The following are MOV instructions and addressing modes:

enter image description here enter image description here

+6
source

Source: https://habr.com/ru/post/1259918/


All Articles