%字母后缺少操作数编号

%字母后缺少操作数编号

本文介绍了错误:无效的"asm":与GCC一起使用内联汇编时,%字母后缺少操作数编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换一个简单的MS汇编代码以与gcc一起使用,我尝试转换的MS汇编就在下面.我有两个int变量,number_return:

I'm tring to convert a simple assembly code of MS to use with gcc, the MS assembly I try to convert is right below. I have two int variables, number and _return:

mov eax, number
neg eax
return, eax

而且,我已经尝试过:

asm("movl %eax, %0" :: "g" ( number));
asm("neg %eax");
asm("movl %0, %%eax" : "=g" ( return ));

但是,编译器给了我这个错误:

But, the compiler gives me this error:

错误在哪里,以及如何解决此错误?谢谢

Where is the error, and, how I can fix this error?Thanks

推荐答案

您不能那样做,因为您要覆盖寄存器而不告知编译器.另外,%是特殊字符,类似于printf.

You can't do it like that because you're overwriting registers without telling the compiler about it. Also, the % is a special character, similar to printf.

最好将所有指令放在一个asm中,否则编译器可能在两者之间做一些意外的事情.

It's also better to put all the instructions in one asm or else the compiler might do something unexpected in between.

尝试以下方法:

asm("movl %%eax, %1\n\t"
    "neg %%eax\n\t"
    "movl %0, %%eax" : "=g" ( _return )  : "g" ( number) : "eax");

不过,可能还有更好的方法:

There's probably a better way, though:

asm("neg %0": "=a" ( _return )  : "a" ( number));

我不知道你为什么不能(在C语言中):

I don't know why you can't just do (in C):

_return = -number;

这篇关于错误:无效的"asm":与GCC一起使用内联汇编时,%字母后缺少操作数编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 23:31