comparing arrays in assembly language programming

[org 0x0100]
mov ax,0
mov bx,7 ;array1 size
mov cx,7 ;array2 size


mov si,0
mov di,0

next1
cmp si,bx ;if all elements in array1 have been compared with the element in array 2
je end1 ;then jump to end with ax=0 ie array2 doesent exist in array1


mov dl,[array1+si] ;element to compare with
mov dh,[array2+di] ;element to compare
add si,1

cmp dl,dh
jne next1 ;go back and compare with next element in array1


;element in array2 found in array1


mov word[temps],si ;move index to memory for to clear si for use
mov word[temps+2],di ;move index to memory for to clear di for use

sub bx,1 ;remove the found element from array1
sub si,1 ;modification in line 22 isnt needed for the time
mov di,si
add di,1


redo: ;code to remove the found element from array1
mov dh,byte[array1+di]
mov byte[array1+si],dh
add si,1
add di,1
cmp si,bx
js redo


;found element removed


mov di,word[temps+2] ;get the index stored in temp back
add di,1 ;element found => index of next element to search
mov si,0
cmp di,cx ;if reached end of array2 ie all elements of array2 are in array1
je end3
jmp next1 ;go back search next element


end3:
mov ax,1 ;array2=array1

end1: ;array2 != array1
mov ax,0x4c00
int 21h

array1: db 1,1,2,3,4,4,5
array2: db 4,4,5,1,1,2,2
temparr: db 0
temps: dw 0

Comments

Popular Posts