Swapping the elements of two arrays

In the following program example we will swap the elements of two arrays.

Program source code

; Author: Evgeny Sergeevich Dovgopolov

array1: db 01h, 02h, 03h, 04h, 05h, 06h, 07h, 08h, 09h, 0Ah
array2: db 11h, 12h, 13h, 14h, 15h, 16h, 17h, 18h, 19h, 1Ah

main:
    lxi     h, array1  ; Load the address of the variable array1 into the HL register pair
    lxi     d, array2  ; Load the address of the variable array2 into the DE register pair
    mvi     c, 0Ah     ; Set the loop counter C to 10

SwapLoop:
    ldax    d          ; Load the value at the memory address (array2) into accumulator A
    mov     b, m       ; Copy the value in memory pointed to by the HL register pair into B
    mov     m, a       ; Copy the value of register A into the memory pointed to by the HL register pair
    mov     a, b       ; Copy the value of register B into register A
    stax    d          ; Store the value in A at the memory address pointed to by DE

    inx     h          ; Increment the HL register pair by 1
    inx     d          ; Increment the value of the DE register pair by 1
    dcr     c          ; Decrement the C counter register by 1
    jnz     SwapLoop   ; Jump to the label SwapLoop until register C becomes 0

    hlt                ; Stop program execution

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".