开发者

assembly code embedded in c giving a improper operand type error for cmp

开发者 https://www.devze.com 2023-04-04 16:24 出处:网络
I have made a progr开发者_如何学Pythonam that is supposed to check whether a number is positive, negative, or zero.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号