Yousaf Raza
Yousaf Raza

Reputation: 793

Array Access Problem in Assembly through data member

I want to move my index in data member and access it by adding the index to starting address of array(mov dx,[bx + i] ) but i gives its address not its value. What should I do?

  ; 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

Upvotes: 0

Views: 167

Answers (1)

zx485
zx485

Reputation: 29052

You cannot use a variable's value in an index expression. So mov dx,[bx + i] is invalid. See Volume 2, Section 2.1.5 on page 509 of the current Intel 64 and IA-32 Architectures Software Development Manual for valid addressing modes of the ModR/M byte (addressing modes) for 16-bit assembly.

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

Yes. That is the correct (but unwanted) behaviour. The addressing mode of this instruction is [BX]+disp16 = [BX + disp16] with mode 10111b (See in the table mentioned in the Instruction Manual). So here disp16 is the address of the variable and not its value. So the instruction is using the address as an index. There is no addressing mode for what you want to achieve. You'd have to put the index into a register first, and then use the appropriate addressing mode from the table.

So change the line

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

to

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

This would generate one correct addressing mode [BX+SI] = 00000b and 010b for the register DX. And according to Figure 2.2 on page 508, the ModR/M byte would be

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

Upvotes: 2

Related Questions