Search code examples
arraysassemblyx86real-mode

x86 real mode: move data from one array to another


Here is my code:

data segment
gio db 1,2,3,4,5,6
ricxvi db 1
jami db 0
x db ?
ends

stack segment
    db   128  dup(0)
ends

code segment
start:   
MOV AX,DATA
MOV DS,AX


     mov cx, 6
     lea si, gio
     mov ah, 0


     n1:
     mov al, [si]
     cmp ricxvi, 3
     je n2
     jmp n3

     n2:
     add jami, al
     mov ricxvi, 1
     jmp n4

     n3:
     add ricxvi, 1
     push ax

     n4:
     add si, 1
     add di, 1
     loop n1




     mov ricxvi, 1
     mov ax, 0
     mov cx, 6
     lea si, gio       


     n5:
     cmp ricxvi, 3
     je n6
     jmp n7


     n6:
     mov ricxvi, 1
     add si, 1
     loop n5

     n7:
     pop [si]
     add si, 1
     loop n5




mov ax, 4c00h
int 21h  

ends

end start

I have a array named gio And I'm trying to reverse this array but leave every 3th element on its position. Meaning I want to get the output like this 5,4,3,2,1,6 but As I inspect variables, in array I have 5,4,2,1,B8. I have noticed that when program first hits pop [si] whole array changes, exploring variables shows me that its 5, NULL, 3, 4, 5, 6 should it not be 5,2,3,4,5,6? I'm using emu8086. Question may sound silly as I'm new with assembly. Thanks.


Solution

  • There are three errors :

    • You are poping two bytes into [si] but you only need one byte. The solution is to pop two bytes into a register and move one byte into [si].
    • Under label n6 you got loop n5, but when cx becomes zero the loop doesn't jump and the block n7 is executed when it shouldn't.
    • You forgot to increase the "3" counter under label n7.

    Here are the fixes :

         n6:
         mov ricxvi, 1
         add si, 1
         loop n5
         jmp finale         ;◄■■ SKIP NEXT BLOCK WHEN CX=0.
    
         n7:
         add ricxvi, 1      ;◄■■ INCREASE THE "3" COUNTER.
         pop ax             ;◄■■ POP TWO BYTES.
         mov [si], al       ;◄■■ USE ONE BYTE ONLY.
         add si, 1
         loop n5
    
    finale:                 ;◄■■ END LABEL.
    
    mov ax, 4c00h
    int 21h