简体   繁体   English

嵌入在C中的汇编代码为cmp提供了错误的操作数类型错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM