---
title: 替换x86 xchgl汇编指令
description: "编译报错：{standard input}: Assembler messages:"
url: https://www.hikunpeng.com/document/detail/zh/kunpenggrf/codeprtr/kunpengtaishanporting_12_0025.html
sourcePath: /source/zh/kunpenggrf/codeprtr/kunpengtaishanporting_12_0025.html
indexId: 114f07548fe3ca878dd2a654c2e4dc1cfd93e69e43e30265bfdf951ad7500d3665
---
# 替换x86 xchgl汇编指令

#### 现象描述

编译报错：{standard input}: Assembler messages:

{standard input}:1222: Error: unknown mnemonic 'xchgl' -- 'xchgl x1,[x19,112]'。


#### 问题原因

xchgl是x86上的汇编指令，作用是交换寄存器/内存变量和寄存器的值，如果交换的两个变量中有内存变量，会对内存变量增加原子锁操作。鲲鹏上可用GCC的原子操作接口__atomic_exchange_n替换。__atomic_exchange_n的第三个入参是内存屏障类型，使用者可以根据自身代码逻辑选择不同的屏障。对多线程访问临界区的逻辑不清晰时，建议使用__ATOMIC_SEQ_CST屏障，避免由屏障使用不当带来一致性问题。


#### 处理步骤

x86实现样例：

```
inline int nBasicAtomicInt::fetchAndStoreOrdered(int newValue)
{
/* 原子操作，把_value的值和newValue交换，且返回_value原来的值
*/
asm volatile("xchgl %0,%1"
: "=r" (newValue), "+m" (m_value)
: "0" (newValue)
: "memory");
return newValue;
}
```


鲲鹏上可替换成：

```
inline int nBasicAtomicInt::fetchAndStoreOrdered(int newValue)
{
/* 原子操作，把_value的值和newValue
* 交换，且返回_value原来的值
*/
return __atomic_exchange_n(&_q_value, newValue, __ATOMIC_SEQ_CST);
}
```
