在 GCC 中调用程序集?

标签 c gcc x86 constraints inline-assembly

#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/

相关文章:

c++ - 将参数传递给通用 lambda 时复制构造函数不正确

gcc - 系统上的 -isystem 包含目录会导致错误

编译AVX2程序

x86 - NASM x86 : send system call interpreting payload to send as NULL

assembly - 如何使用缓冲区溢出攻击替换堆栈上的返回地址

c - "unsigned int *ptr"和 signed int *ptr” 有什么区别?

c - gethostbyname,连接到整个互联网?

android - 将 C 代码转换为 JNI C 代码

c - 读取字符串时导致总线错误

c - ELF 二进制分析静态与动态。汇编代码如何|指令内存映射变化?