#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 = 新值;返回一个
最佳答案
=
和 +
是约束修饰符。
http://gcc.gnu.org/onlinedocs/gcc/Modifiers.html#Modifiers
`=' Means that this operand is write-only for this instruction: the previous value is discarded and replaced by output data.
`+' Means that this operand is both read and written by the instruction.
基本约束在这里
http://gcc.gnu.org/onlinedocs/gcc/Simple-Constraints.html#Simple-Constraints
m
A memory operand is allowed, with any kind of address that the machine supports in general...
1
.. An operand that matches the specified operand number is allowed. If a digit is used together with letters within the same alternative, the digit should come last.
'a' 是 i386 特定的
http://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html#Machine-Constraints
a The a (eax) register.
关于在 GCC 中调用程序集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2489824/