Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASM: operand type mismatch for `cmp'

Tags:

x86

assembly

I'm doing x86 asembly code and I keep getting this Error: operand type mismatch for `cmp's

The line of code it appears at is:

cmpb %rdi, $0
like image 328
Tyree Jackson Avatar asked Mar 05 '23 06:03

Tyree Jackson


1 Answers

In AT&T syntax (which is what you use), instructions have a size suffix to indicate the operand size. The size suffixes are:

b byte        1 bytes
w word        2 bytes
l long        4 bytes
q quad-word   8 bytes

s single      4 bytes
d double      8 bytes
t temporary  10 bytes

For example, cmpb is the instruction cmp with a 1 byte operand size indicated. However, your code uses %rdi as an operand which is a quad-word (64 bit) register, so the assembler rightfully complains that this is the wrong operand.

To fix this issue, simply leave out the size suffix; the assembler is able to infer it unless all operands are immediates or memory operands:

cmp %rdi, $0

You can of course also explicitly supply a size suffix; in this case, q is appropriate as indicated in the previous table:

cmpq %rdi, $0

That said, note that as with most instructions, the immediate operand has to be the first operand to cmpq:

cmpq $0, %rdi

The other form is actually illegal.

like image 143
fuz Avatar answered Mar 25 '23 01:03

fuz