问题描述
我正在执行 x86 汇编代码,但不断收到此错误:`cmp 的操作数类型不匹配
I'm doing x86 asembly code and I keep getting this Error: operand type mismatch for `cmp's
它出现的代码行是:
cmpb %rdi, $0
推荐答案
在 AT&T 语法(您使用的语法)中,指令有一个大小后缀来指示操作数的大小.尺寸后缀为:
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
例如,cmpb
是指令 cmp
,其中指示了 1 个字节的操作数大小.但是,您的代码使用 %rdi
作为操作数,它是一个四字(64 位)寄存器,因此汇编器理所当然地抱怨这是错误的操作数.
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
您当然也可以明确提供尺寸后缀;在这种情况下,q
是合适的,如上表所示:
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
也就是说,请注意,与大多数指令一样,立即操作数必须是 cmpq
的第一个操作数:
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.
这篇关于ASM:“cmp"的操作数类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!