Rate This Document
Findability
Accuracy
Completeness
Readability

Replacing the x86 xchgl Assembly Instruction

Symptom

Error: {standard input}: Assembler messages:

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

Cause

xchgl is an x86 assembly instruction. It is used to exchange the values of (register/memory variable) and (register). If the two exchanged variables contain a memory variable, an atomic lock operation is added to the memory variable. On the Kunpeng platform, you can use the GCC atomic operation interface __atomic_exchange_n instead. The third input parameter of __atomic_exchange_n is the memory barrier type. You can select a different barrier based on the code logic. If the logic for multiple threads to access the critical area is unclear, you are advised to use the __ATOMIC_SEQ_CST barrier to avoid consistency problems caused by improper use of the barrier.

Procedure

Example code on the x86 platform:

inline int nBasicAtomicInt::fetchAndStoreOrdered(int newValue)
{
    /*Atomic operation. Exchanges the value of _value with that of newValue and returns the original value of _value.
     */



    asm volatile("xchgl %0,%1"
                 : "=r" (newValue), "+m" (m_value)
                 : "0" (newValue)
                 : "memory");
    return newValue;
}

Replace the code as follows on the Kunpeng platform:

inline int nBasicAtomicInt::fetchAndStoreOrdered(int newValue)
{
    /* Atomic operation.
     * Exchanges the values of _value and newValue and returns the original value of _value.
     */
    return __atomic_exchange_n(&_q_value, newValue, __ATOMIC_SEQ_CST);
}