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

#### 现象描述

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

{standard input}:1222: Error: unknown mnemonic 'cmpxchgl '。


#### 问题原因

与xchgl类似，cmpxchgl是x86上的汇编指令，作用是比较并交换操作数。鲲鹏上无对应指令，可用GCC的原子操作接口__atomic_compare_exchange_n进行替换。


#### 处理步骤

x86实现样例：

```
inline bool nBasicAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
{
unsigned char ret;
/* 原子操作，原来m_value的值如果等于expectedValue,则把newValue
* 载入m_value，且返回ret=true; 如果不等于，则m_value的值不变，且返回ret=false
*/
asm volatile("lock\n"
"cmpxchgl %3,%2\n"
"sete %1\n"
: "=a" (newValue), "=qm" (ret), "+m" (m_value)
: "r" (newValue), "0" (expectedValue)
: "memory");
return ret != 0;
}
```

鲲鹏上可替换成：

```
inline bool nBasicAtomicInt::testAndSetOrdered(int expectedValue, int newValue)
{
unsigned char ret;
/* 原子操作，原来m_value的值如果等于expectedValue，则把newValue载入_value，且返回ret=true; 如果不等于，则m_value的值不变，且返回ret=false */
return __atomic_compare_exchange_n(&m_value, &expectedValue, newValue, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
}
```
