Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more: (untagged)
how do you compare array elements?
lets say i have a variable named row and its value is 45
then an arr db 42,41

cmp row, arr[0] or cmp row, [arr]

both doesnt seem to work. or do i have to move the contents to a variable?? then use cmp?
Posted

1 solution

You have to use a register as index to move in the array:
mov si, arr  ;register si points to base array
xor bx,bx    ;BX=0
mov al, BYTE PTR[ROW]
cmp al, BYTE PTR[SI+BX]  ;make comparison, address is SI(=arr)+BX(=index)
je  IsEqual  ;Match

For more info's you can see this[^].
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 7-Mar-15 13:50pm    
Looks correct at first glance, a 5. Did you validate it? It's a luck that this simple code is possible in the protected mode under modern OS. :-)
—SA
Frankie-C 7-Mar-15 15:49pm    
Yes, it works, but is 8086 pure 16 bits code.
You cannot compile it under a 32 bits OS because of the pointers size.
To use it under 32 bits OS you have to change pointers size:

mov esi, arr ;register si points to base array
xor bx,bx ;BX=0
mov al, BYTE PTR[ROW]
cmp al, BYTE PTR[ESI+BX] ;make comparison, address is SI(=arr)+BX(=index)
je IsEqual ;Match

For 64 bits OS:

mov rsi, arr ;register si points to base array
xor bx,bx ;BX=0
mov al, BYTE PTR[ROW]
cmp al, BYTE PTR[RSI+BX] ;make comparison, address is RSI(=arr)+BX(=index)
je IsEqual ;Match

Thanks for the 5...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900