简体   繁体   中英

What's the meaning of the following code?

There is a CAS code below which can handle just int type,I know the function of CAS but I don't know the details shown below.

inline int CAS(unsigned long *mem,unsigned long newval,unsigned long oldval)
{
    __typeof (*mem) ret;
    __asm __volatile ("lock; cmpxchgl %2, %1"
    : "=a" (ret), "=m" (*mem)
    : "r" (newval), "m" (*mem), "0" (oldval));
    return (int) ret;
}

I know there should be five parameters mapped to %0,%1,%2,%3,%4 because there are five parameters in input/output field

I also know that "=a" means using eax register, "=m" means using memory address, "r" means using any register

But I don't understand what the "0" means.

I don't understand why "cmpxchgl" only use two parameters %2, %1 instead of three?

It should use three params as the CAS function.

Where can I get all the infimation about the inline c asm?I need a complete tutorial.

%2 is newval , %1 is *mem

with "0" (oldval) , and the first register occur is "=a" , means that oldval is stored in eax .

So cmpxchgl %2, %1" means cmpxchgl newval, *mem" (while oldval in eax ), which checks eax (value of oldval) whether equals *mem , if equal, change value of *mem to newval .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM