Search code examples
assemblyx86tasm

AX in gets strange number from word array


i'm trying to sub two arrays to create third one, but when i try to sub them the value the AX register get is completely different than the original.
in the first sub, as you can see, i'm trying to move the number 2082(822 h) to AX but in TD it shows AX get something like 32849(8052 h) . what's wrong??? thanks!!!

.MODEL SMALL
.STACK 100h
.DATA
ARR1   DW 333,20989,3456,2082
ARR2   DW 333,15,5436,2082
ARR3   DW ?
ANSWER DB 'The last digit is: X' ,13,10,'$'
TEN    DW 10


.CODE
     MOV AX,@DATA   ; DS can be written to only through a register
     MOV DS,AX      ; Set DS to point to data segment

     ; Making the first arr3 number

     MOV DI,3
 MOV AX,0
 MOV AX, ARR1[DI]
 SUB AX,ARR2[0]
 MOV ARR3[0], AX
 MOV AX,0

Solution

  • Each data value DW is stored as 2 bytes, with the least significant byte first (little-endian). But you are using an offset of 3 which does not align with the data.

    Your data values will appear in memory as

    ARR1    4D 01 FD 51 80 0D 22 08
    ...              ^^ ^^
    

    By loading from offset 3 register AX will receive the value 8051 ( little-endian) which in decimal is 32849.

    You should be using an offset of 6 to read the last element in that array. Alternatively (when using 32-bit mode) you can scale the offset with

    MOV EAX, ARR1[EDI*2]