通过数据成员访问程序集中的数组问题

Array Access Problem in Assembly through data member

我想移动数据成员中的索引并通过将索引添加到数组的起始地址(mov dx,[bx + i]) 来访问它,但我给出的是它的地址而不是它的值。我该怎么办?

  ; This method of array access 
    mov ah,2
lea bx,arr ; load address of DW array
mov i,2 ; DW data member
int 21h
mov dx,[bx + i] ; this line is having problem
add dx,30h
int 21h

您不能在索引表达式中使用变量值。所以 mov dx,[bx + i] 是无效的。有关 ModR/M 的有效寻址模式,请参阅当前 Intel 64 and IA-32 Architectures Software Development Manual 第 2 卷第 2.1.5 节 第 509 页 16 位汇编的字节(寻址模式)。

That's where I'm confused, assembler does not give any severe error and assembles successfully. What happens instead of putting value of 'i' it puts address of i

是的。这是正确的(但不需要的)行为。该指令的寻址方式为[BX]+disp16 = [BX + disp16],寻址方式为10111b(见说明书中提到的table)。所以这里的 disp16 是变量的地址而不是它的值。 所以指令使用地址作为索引。您想要实现的目标没有寻址模式。您必须先将索引放入寄存器,然后使用 table.

中的适当寻址模式

所以换行

mov dx, [bx + i]   ; this line is having problem

mov si, i          ; i is a WORD variable
mov dx, [bx+si]    ; correct addressing mode

这将为寄存器DX生成一个正确的寻址模式[BX+SI] = 00000b010b。根据第 508 页的 图 2.2ModR/M 字节 将是

Mod 00......
R/M .....000
Reg ..010...
=== 00010000 = 10h