c内联汇编得到“操作数大小不匹配";使用cmpxchg时 [英] c inline assembly getting "operand size mismatch" when using cmpxchg
问题描述
我正在尝试将cmpxchg与c内联汇编一起使用.这是我的代码:
I'm trying to use cmpxchg with inline assembly through c. This is my code:
static inline int
cas(volatile void* addr, int expected, int newval) {
int ret;
asm volatile("movl %2 , %%eax\n\t"
"lock; cmpxchg %0, %3\n\t"
"pushfl\n\t"
"popl %1\n\t"
"and $0x0040, %1\n\t"
: "+m" (*(int*)addr), "=r" (ret)
: "r" (expected), "r" (newval)
: "%eax"
);
return ret;
}
这是我第一次使用内联,我不确定是什么原因导致此问题. 我也尝试过"cmpxchgl",但还是没有.还尝试了解除锁定. 我收到操作数大小不匹配". 我认为可能与我对addr进行的投射有关,但我不确定.我尝试将int替换为int,所以不真正理解为什么会有大小不匹配的情况. 这是使用AT& T样式. 谢谢
This is my first time using inline and i'm not sure what could be causing this problem. I tried "cmpxchgl" as well, but still nothing. Also tried removing the lock. I get "operand size mismatch". I think maybe it has something to do with the casting i do to addr, but i'm unsure. I try and exchange int for int, so don't really understand why there would be a size mismatch. This is using AT&T style. Thanks
推荐答案
您已颠倒了cmpxchg指令的操作数顺序. AT& T语法最后需要存储目标:
You had the operand order for the cmpxchg instruction is reversed. AT&T syntax needs the memory destination last:
"lock; cmpxchg %3, %0\n\t"
或者您可以使用-masm=intel
将该指令按照其原始顺序进行编译,但是您的其余代码是AT& T语法和顺序,因此这不是正确的答案.
Or you could compile that instruction with its original order using -masm=intel
, but the rest of your code is AT&T syntax and ordering so that's not the right answer.
至于为什么它说操作数大小不匹配",我只能说这似乎是一个汇编程序错误,因为它使用了错误的消息.
As far as why it says "operand size mismatch", I can only say that that appears to be an assembler bug, in that it uses the wrong message.
这篇关于c内联汇编得到“操作数大小不匹配";使用cmpxchg时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!