Comparison of two arrays
In the following program example we will compare two arrays. The program returns in the accumulator the index where the elements were not equal. If the arrays are equal, it returns -1 in the accumulator.
Program source code
; Author: Evgeny Sergeevich Dovgopolov array1: db 01h, 02h, 03h, 04h, 05h, 06h, 07h, 08h, 09h, 0Ah array2: db 01h, 02h, 03h, 01h, 05h, 06h, 07h, 08h, 09h, 0Ah result: db 00h 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 b, 00h ; Mismatch index mvi c, 0Ah ; Set the loop counter C to 10 CompareLoop: ldax d ; Load the value at the memory address (array2) into accumulator A cmp m ; Compare the value in register A with the value in the memory location jnz Unequal ; If Z=0, then jump to the label Unequal inx h ; Increment the HL register pair by 1 inx d ; Increment the value of the DE register pair by 1 inr b ; Increment the value in the index register B by 1 dcr c ; Decrement the C counter register by 1 jnz CompareLoop ; Jump to the label CompareLoop until register C becomes 0 mvi a, 0FFh ; If the elements are not equal, return the value -1 in the accumulator hlt ; Stop program execution Unequal: mov a, b ; If the elements are equal, return its index in the array in the accumulator sta result ; Save the value of register A in memory (result) hlt ; Stop program execution
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".