yangyixiaof
yangyixiaof

Reputation: 257

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.

Upvotes: 2

Views: 1178

Answers (1)

xinnjie
xinnjie

Reputation: 732

%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.

Upvotes: 2

Related Questions