assembly code embedded in c giving a improper operand type error for cmp
I have made a progr开发者_如何学Pythonam 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. 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. You need to use cmp eax, 0
instead. This would also be consistent with your conditional jumps (jl
would jump if eax
is negative, etc.).
You may be confusing Intel assembly syntax (which you used) with AT&T syntax, where the order of operands is reversed.
Additionally, your usage of ret
is incorrect: ret
is used to return from a function, but there is no function call here. What you need there is a jmp
to the mov output, ebx
line.
You cannot have an immediate as the first operand to cmp
. You need to do cmp eax, 0
instead.
The syntax of cmp for comparing a register against a constant requires the constant to come second. So cmp eax, 0
should be fine.
Valid combinations are:
cmp reg, reg
cmp reg, mem
cmp mem, reg
cmp reg, const
精彩评论