Tyree Jackson
Tyree Jackson

Reputation: 41

ASM: operand type mismatch for `cmp'

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

Upvotes: 4

Views: 7961

Answers (1)

fuz
fuz

Reputation: 93127

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.

Upvotes: 7

Related Questions