Search code examples
assemblyx86-16emu8086

Accessing elements in an array and store each element in a register in Assembly 8086


I'm trying to access an array and with every iteration I store the value of the element in register AL. The array I am accessing is an array that got its values from the user, the values I entered are (12134).

The issue is when I try to access the each element of the array in the procedure and store in register AL, the wrong value is stored which is (31) and this is for the first element, the second element is stored as (32) in AL. I tried to know the reason behind this value but couldn't figure it out. Thanks in advance.

Note: I have provided screenshots of my registers on the first iteration for further clarification along with the code

;read and access array

DIM EQU 5 

.MODEL small
.STACK
.DATA   

arr DB DIM DUP(?)  

.CODE
.STARTUP  

MOV BX, OFFSET arr  ;Storing the address of arr to bx
CALL READ           ; Calling the read procedure to input data into arr

MOV BX, OFFSET arr  ;Storing the address of arr to bx
CALL arr_acc        ;Calling the procedure that should access each element of the array and store each element
                    ;in AL with every iteration
       
.EXIT

; PROCEDURES

; Read PROC 
    
MOV DI, 0 
MOV AH, 1 ; set AH for reading 

lab1:   INT 21H ; read a character
        MOV BX[DI], AL ; store the character
        INC DI ; go to next element
        CMP DI, DIM ; compare array index with 0
        JNE lab1 ; jump if not equal

RET 

Read ENDP  

arr_acc PROC
     
MOV AX,0
MOV DI,0 

lop: CMP DI,DIM                ; checking if the DI reached the maximum index DIM
     JE  EXIT_LOP              ;exit the lop which will exit the procedure as well
     MOV AL, BX[DI]            ; Moving the first element in AL
     INC DI                    ; increment DI to get to the next element
     JMP lop                   ; jump back to the loop
                                
EXIT_LOP:    

RET
arr_acc ENDP 

END

enter image description here

enter image description here

Attempts:

  1. I tried to use SI register instead of DI
  2. Used only BX and incremented it by 1 since its an array of bytes

Solution

  • got its values from the user, the values I entered are (12134). the wrong value is stored which is (31) and this is for the first element, the second element is stored as (32) in AL

    Think a moment about what the reader of your question has to deal with here! Your numbers between parenthesis (12134, 31, and 32) all look like plain decimal numbers, but only an expert reader will see through this and understand that 31 and 32 would be hexadecimal numbers that you got from the debugger screen. Be kind to the reader and write these like 31h and 32h. Alternatively use 0x31 and 0x32.

    Why there is a problem

    The DOS.GetCharacter function 01h responds with a character code (ASCII). For the keys 0 to 9, you get ASCII codes in the range 48 to 57. Before you use the input, you should convert from character code to the nominal value of the digit involved. The conversion is simply to subtract 48, which we normally write as sub al, '0'.