在 GCC 中调用汇编?
#include <stdlib.h>
static inline uint
xchg(volatile unsigned int *addr, unsigned int newval)
{
uint result;
asm volatile("lock; xchgl %0, %1" : "+m" (*addr), "=a" (result) : "1" (newval) : "cc");
return result;
}
有人可以告诉我这段代码到底是做什么的吗?我的意思是我有一个想法或这个命令的一部分。 “1”newval是输入,“=a”是刷新它之前的值并更新它。 “m”用于内存操作,但我对此函数的功能感到困惑。 “+m”符号有什么作用?这个函数是否执行类似 m=a; 的操作m = 新值;返回一个
#include <stdlib.h>
static inline uint
xchg(volatile unsigned int *addr, unsigned int newval)
{
uint result;
asm volatile("lock; xchgl %0, %1" : "+m" (*addr), "=a" (result) : "1" (newval) : "cc");
return result;
}
Can some one tell me what this code does exactly? I mean I have an idea or the parts of this command. "1" newval is the input, "=a" is to flush out its previous value and update it. "m" is for the memory operation but I am confused about the functionality of this function. What does the "+m" sign do? Does this function do something like m=a; m = newval; return a
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
=
和+
是约束修饰符。http://gcc.gnu.org/onlinedocs/gcc/Modifiers.html#修饰符
基本约束在这里
http://gcc.gnu.org/ onlinedocs/gcc/Simple-Constraints.html#Simple-Constraints
'a' 是 i386 特定的
http://gcc.gnu。 org/onlinedocs/gcc/Machine-Constraints.html#Machine-Constraints
=
and+
are constraint modifiers.http://gcc.gnu.org/onlinedocs/gcc/Modifiers.html#Modifiers
Basic constrains are here
http://gcc.gnu.org/onlinedocs/gcc/Simple-Constraints.html#Simple-Constraints
'a' is i386 specific
http://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html#Machine-Constraints
有
限制
。它们不是变量,而是模式,例如fopen()
的“w”、“r”、“r+”,其中一些描述如下
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
第 6 部分。有关约束的更多信息。
最后一个“cc”是“clobber”。
asm的完整格式是
there are
constraints
. They are not variables, but modes, like the "w", "r", "r+" offopen()
some of them are described here
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
part 6. More about constraints.
And the last "cc" is clobber.
Full format of asm is