0

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
2
  • 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 Commented May 4, 2019 at 19:35
  • 1
    Keep your index in a register and you wouldn't have this problem in the first place. That's what registers are for. Commented May 5, 2019 at 0:30

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.