简体   繁体   English

以下代码的含义是什么?

[英]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. 下面有一个CAS代码,它只能处理int类型,我知道CAS的功能,但是我不知道下面显示的细节。

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 我知道应该有五个参数映射到%0,%1,%2,%3,%4,因为在输入/输出字段中有五个参数

I also know that "=a" means using eax register, "=m" means using memory address, "r" means using any register 我也知道"=a"表示使用eax寄存器, "=m"表示使用内存地址, "r"表示使用任何寄存器

But I don't understand what the "0" means. 但是我不明白“ 0”的含义。

I don't understand why "cmpxchgl" only use two parameters %2, %1 instead of three? 我不明白为什么“ cmpxchgl”仅使用两个参数%2,%1而不是三个?

It should use three params as the CAS function. 它应该使用三个参数作为CAS函数。

Where can I get all the infimation about the inline c asm?I need a complete tutorial. 我在哪里可以获得有关内联c asm的所有信息?我需要完整的教程。

%2 is newval , %1 is *mem %2newval%1*mem

with "0" (oldval) , and the first register occur is "=a" , means that oldval is stored in eax . "0" (oldval) ,并且第一个寄存器为"=a" ,表示oldval存储在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 . 因此, cmpxchgl %2, %1"表示cmpxchgl newval, *mem" (而eax oldval ),它检查eax (oldval的值)是否等于*mem ,如果相等,则将*mem值更改为newval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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