How to compare two words in NASM assembly? consider this code:
global start
start:
mov eax,array
mov edx,4
mov ecx,2987074
.LOOP1:
cmp word [eax],ecx
je .FOUND
add eax,4
sub edx,1
jz .NOTFOUND
jmp .LOOP1
.FOUND:
xor ebx,ebx
jmp .EXIT
.NOTFOUND:
mov ebx,1
.EXIT:
mov eax,1
int 0x80
array:
dd 1137620
dd 3529469
dd 2987074
dd 1111111
dd 2222222
returns
foo.asm:7: error: mismatch in operand sizes
and changing cmp from cmp word [eax],ecx to cmp word [eax],word ecx
returns:
foo.asm:7: warning: register size specification ignored
foo.asm:7: error: mismatch in operand sizes
I have no idea how to fix this. Can someone explain it?
cmp word [eax],ecxis wrong because operand sizes don’t match (ecxis adword, notword). Most x86 instructions with two operands can only work with operands of the same size.cmp word [eax],word ecxis wrong becauseecxis adword, notword.If you’re coming from the
(g)as/gccworld, it’s worth noting that their.wordis the machine word and on a 32-bit machine it’s 32-bit. NASM’swordis always 16-bit and itsdwordis always 32-bit.You probably want just
cmp [eax], ecx. Since the two operands ofcmpmust be of the same size, NASM deduces here that the memory operand at address ineaxis the same size as the register operandecx, 32-bit (dword).