What does the sequence of x86 instructions "neg rax; sbb rax,rax; neg rax" do?
This instruction sequence is a clever branchless way to convert a value to a boolean (0 or 1):
Step-by-step breakdown:
1. neg rax - Negates rax (two's complement)
• If rax == 0: result is 0, carry flag CF = 0
• If rax != 0: result is -rax, carry flag CF = 1
2. sbb rax, rax - Subtract with borrow: rax = rax - rax - CF
• If CF == 0: rax = 0 - 0 - 0 = 0
• If CF == 1: rax = 0 - 0 - 1 = -1 (0xFFFFFFFFFFFFFFFF)
3. neg rax - Negates rax again
• If rax == 0: result is 0
• If rax == -1: result is 1
This is essentially computing rax = (rax != 0) ? 1 : 0 without any branches. It's a classic trick used in performance-critical
code where branch mispredictions would be costly, converting any non-zero value to exactly 1.
What does the sequence of x86 instructions ".loop: xadd rax,rdx; loop .loop" do?
This instruction sequence computes Fibonacci numbers (or a Fibonacci-like recurrence):
Breakdown:
1. xadd rax, rdx - Atomic exchange-and-add:
• Saves old rax to a temp location
• rax = rax + rdx
• rdx = temp (old value of rax)
2. loop .loop - Decrements rcx and jumps to .loop if rcx != 0
The coefficients follow the Fibonacci sequence!
Classic use case:
mov rax, 1;
mov rdx, 0;
mov rcx, N;
.loop: xadd rax, rdx;
loop .loop
After N iterations:
• rax = F(N+2) (the (N+2)th Fibonacci number)
• rdx = F(N+1) (the (N+1)th Fibonacci number)
This is a compact, branchless way to compute Fibonacci numbers, though the loop instruction itself is somewhat slow on modern
processors, so it's more of a code golf trick than production code.
Грок тоже не облажался. Дал те же ответы, развёрнуто, с пояснениями. Первый вопрос занял 22 секунды, второй 58 секунд. Вкратце:
This x86-64 sequence (neg rax; sbb rax, rax; neg rax) converts the original 64-bit value in RAX to 1 if it was non-zero, or 0 if it was zero. It is a branchless "boolean cast" (like !!x or (x != 0) ? 1 : 0 in C) that works for both positive and negative values.
This x86-64 sequence (.loop: xadd rax,rdx; loop .loop) is a famous 2-instruction Fibonacci generator (snippet 0x01 from xorpd’s book xchg rax, rax). It advances the pair (RAX, RDX) along the Fibonacci recurrence exactly N times, where N = initial RCX (RCX must be > 0; RCX = 0 would infinite-loop).
no subject
Date: 2026-02-25 21:09 (UTC)no subject
Date: 2026-02-25 21:37 (UTC)https://vak.dreamwidth.org/280343.html
no subject
Date: 2026-02-25 22:17 (UTC)no subject
Date: 2026-02-25 22:18 (UTC)no subject
Date: 2026-02-25 22:29 (UTC)Cursor в состоянии достичь нирваны:
no subject
Date: 2026-02-25 23:24 (UTC)Теперь есть у кого спрашивать такие вещи. 😀
Пойду помучаю грока этим делом...
no subject
Date: 2026-02-25 23:27 (UTC)This x86-64 sequence (neg rax; sbb rax, rax; neg rax) converts the original 64-bit value in RAX to 1 if it was non-zero, or 0 if it was zero. It is a branchless "boolean cast" (like !!x or (x != 0) ? 1 : 0 in C) that works for both positive and negative values.
This x86-64 sequence (.loop: xadd rax,rdx; loop .loop) is a famous 2-instruction Fibonacci generator (snippet 0x01 from xorpd’s book xchg rax, rax).
It advances the pair (RAX, RDX) along the Fibonacci recurrence exactly N times, where N = initial RCX (RCX must be > 0; RCX = 0 would infinite-loop).