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

Use of carry flag for SBB RCX,RCX after subtraction

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

sub      rdx,rax
sbb      rcx,rcx
and      rcx,rdx
add      rax,rcx

一周以来我一直在研究这个(每天几分钟)。我研究了一些试图解决它的问题:数字的符号表示、减法的工作原理、减法后 CF 的作用。根据this answer and the list given here。我看不出在减法后检查 CF 的意义,除非在可能溢出的情况下。

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

实际上,该代码是一种聪明的无分支方式 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

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

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:

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