TousakaRin commented on issue #2489:
URL: https://github.com/apache/brpc/issues/2489#issuecomment-1873774338

   看了看文档,我尝试翻译一下:
   - [lock](https://www.felixcloutier.com/x86/lock) 前缀表示后续的指令是原子操作
   - [xaddl src dst](https://www.felixcloutier.com/x86/xadd):  等价于: temp = src 
+ dst; src = dst; dst = temp; 也就是 fetch_add。
   - `+r (temp), +m(*ptr)`, 这一句是表明这段汇编代码的输出操作数,也就是这个汇编代码会改变的变量,其中 temp 
由编译器选一个寄存器保存,记为%0,*ptr 则需要直接访问内存,记为 %1
   - `lock xaddl %0, %1`,这里注意gcc的汇编语法第一个数是src操作数,intel语法第一个是dst操作数。所以 %0 
对应xaddl 指令中的src, %1 对应 dst
   
   综上所述,这段汇编代码翻译过来就是
   ```c++
   Atomic32 temp = increment;
   
   // 以下三行是原子操作
   Atomic32 asm_temp = temp + *ptr;
   temp = *ptr;
   *ptr = asm_temp;
   
   return temp + increment
   ```
   所以说,ptr 中保存的就是相加后的值。对gcc内嵌汇编也不是特别熟悉,有错误欢迎指正。
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org
For additional commands, e-mail: dev-h...@brpc.apache.org

Reply via email to