[英]assembly code embedded in c giving a improper operand type error for cmp
I have made a program that is supposed to check whether a number is positive, negative, or zero. 我制作了一个程序,该程序应检查数字是正数,负数还是零。 When I try to compile the code, it gives a improper operand type error, for line 28, which is the cmp opcode.
当我尝试编译代码时,对于第28行(即cmp操作码),它给出了不正确的操作数类型错误。 Am I formatting it wrong, or is there some other problem here?
我格式化错误,还是这里还有其他问题?
#include <stdio.h>
int input;
int output;
int main (void)
{
scanf("%d", &input);
__asm
{
jmp start
negative:
mov ebx, -1
ret
nuetral:
mov ebx, 0
ret
positive:
mov ebx, 1
ret
start:
mov eax, input
mov ebx, other
cmp 0, eax
jl negative
je neutral
jg positive
mov output, ebx
}
printf("%d\n", output);
}
The first operand of the cmp
instruction must be a register or a memory location, not an immediate value. cmp
指令的第一个操作数必须是寄存器或存储器位置,而不是立即数。 You need to use cmp eax, 0
instead. 您需要使用
cmp eax, 0
而不是cmp eax, 0
。 This would also be consistent with your conditional jumps ( jl
would jump if eax
is negative, etc.). 这也将与您的条件跳转一致(如果
eax
为负,则jl
会跳转,等等)。
You may be confusing Intel assembly syntax (which you used) with AT&T syntax, where the order of operands is reversed. 您可能会使Intel汇编语法(您使用过的)与AT&T语法混淆,在AT&T语法中,操作数的顺序相反。
Additionally, your usage of ret
is incorrect: ret
is used to return from a function, but there is no function call here. 另外,您对
ret
的使用是不正确的: ret
用于从函数返回,但是这里没有函数调用。 What you need there is a jmp
to the mov output, ebx
line. 您需要的是
mov output, ebx
行的jmp
。
You cannot have an immediate as the first operand to cmp
. 您不能将立即数作为
cmp
的第一个操作数。 You need to do cmp eax, 0
instead. 您需要执行
cmp eax, 0
而不是cmp eax, 0
。
The syntax of cmp for comparing a register against a constant requires the constant to come second. 用于将寄存器与常量进行比较的cmp语法要求常量排在第二位。 So
cmp eax, 0
should be fine. 所以
cmp eax, 0
应该没问题。
Valid combinations are: 有效组合为:
cmp reg, reg
cmp reg, mem
cmp mem, reg
cmp reg, const
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.