减法后使用SBB RCX,RCX的进位标志

Ane*_*gra 3 assembly x86-64

我正在读一本书:[xchg rax,rax].以下是本书的0x03片段,我无法理解这一点.

sub      rdx,rax
sbb      rcx,rcx
and      rcx,rdx
add      rax,rcx
Run Code Online (Sandbox Code Playgroud)

我一直在工作一周以来(每天几分钟).我研究了一些试图解决它的事情:数字的有符号表示,减法如何工作,减法后CF的作用.根据这个答案和这里给出的清单.除了可能溢出的情况之外,我没有看到减法后检查CF的重点.

在什么情况下检查进位标志在减法后有用?

Jes*_*ter 5

实际上,代码是一种聪明的无分支方式rax = min(rax, rdx).

sub rdx, rax ; rdx = rdx - rax; CF set if rdx < rax
sbb rcx, rcx ; rcx = all 1 bits if CF was set, 0 otherwise
and rcx, rdx ; rcx = rdx - rax if CF was set, 0 otherwise
add rax, rcx ; rax = rax + (rdx - rax) = rdx if CF was set, unchanged otherwise
Run Code Online (Sandbox Code Playgroud)

更具可读性的分支版本是:

cmp rdx, rax
jnc done ; if rdx - rax produced no carry, rax is smaller or equal
mov rax, rdx ; otherwise rdx is the smaller one
done:
Run Code Online (Sandbox Code Playgroud)

它仍然只是使用CF进行溢出检查.