"XCHG RAX, RAX" is a kind of riddle book that provides assembly code for you to reverse and undercover the meaning. Some of the examples calculate the Fibonacci sequence others bit-twiddle to toggle ASCII case. The snippet (riddle) on 0x03 is,

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

How does this code work, and what does it do?


This code boils down to,

rax = min(rdx,rax)
rdx = sub(rdx,rax) ; store the difference in rdx

That is essentially,

  • rdx - 0 (if rdx is the min)
  • rdx - (rdx-rax) (if rax is the min)

The sbb and and here just move into rcx either

  • 0
  • rdx-rax

What determines what gets moved into rcx? That's determined by the result of the sbb. The sbb is doing reg - reg - CF. So you're either ANDing against all 1s or all 0s.

This is how I reasoned about it

# CF=0; rdx > rax
if ( rdx > rax ) {
  rdx -= rax
  rcx = 0     ; all bits off

              ; AND 0 (rcx) with anything (in rdx) is nop here.
              ; ADDing 0 (rcx) to rax is a nop

# CF=1; rax > rdx
else {
  rdx -= rax
  rcx = -1    ; all bits on

  rcx = rdx   ; code is rcx &= rdx
              ; remember -1 & x == x
  rax += rcx


Note regardless of the carry flag, this code will store the difference in rdx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.