TExcelTAD
Home › x86-64 Assembly › Conditional Moves

Conditional Moves

A conditional jump that the CPU guesses wrong costs around 15–20 cycles, because the processor has already started executing instructions on the predicted path and must throw them away. For simple choices like "take the larger of two values" there is a branch-free alternative: cmovcc, a mov that happens only if a condition holds.

The cmov family

cmovcc dest, src copies src into dest if condition cc is true; otherwise it does nothing (but see the 32-bit note below). The condition codes are the same as for jumps:

Instruction Moves if (after cmp a, b)
cmove / cmovz equal
cmovne / cmovnz not equal
cmovl, cmovle, cmovg, cmovge signed less, ≤, greater, ≥
cmovb, cmovbe, cmova, cmovae unsigned below, ≤, above, ≥
cmovs, cmovns negative, not negative

Restrictions: the destination must be a register (16, 32 or 64 bits — no 8-bit form), and the source a register or memory, never an immediate.

max and min without jumps

nasm · try it yourself
section .text
global _start
_start:
    mov edi, 17
    mov esi, 42
    call max
    mov rdi, rax
    call print_int

    mov rdi, -8
    mov rsi, -3
    call min
    mov rdi, rax
    call print_int

    mov eax, 60
    xor edi, edi
    syscall

; max(a, b) -> the larger (signed)
max:
    mov rax, rdi                ; assume a
    cmp rdi, rsi
    cmovl rax, rsi              ; if a < b, take b
    ret

; min(a, b) -> the smaller (signed)
min:
    mov rax, rdi
    cmp rdi, rsi
    cmovg rax, rsi              ; if a > b, take b
    ret

; ---- print_int: print rdi as a signed decimal number and a newline ----
print_int:
    test rdi, rdi
    jns print_uint          ; not negative: print it as it is
    neg rdi                 ; make it positive
    push rdi                ; keep it (write and syscall clobber registers)
    push '-'                ; the minus sign, as a byte on the stack
    mov eax, 1              ; write(1, rsp, 1)
    mov edi, 1
    mov rsi, rsp
    mov edx, 1
    syscall
    pop rdi                 ; drop the '-'
    pop rdi                 ; the positive value again
    jmp print_uint          ; print_uint's ret returns to our caller

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
42
-8

The pattern is always the same: compute a default result, compare, then conditionally overwrite it.

Clamping a value to a range

clamp(x, low, high) limits x to [low, high]. Two comparisons, two conditional moves, zero jumps — a typical operation in graphics and audio code:

nasm · try it yourself
section .data
samples:    dq -40, 15, 130, 99, 100, 0, 250
COUNT       equ ($ - samples) / 8

section .text
global _start
_start:
    xor ebx, ebx
.next:
    mov rdi, [samples + rbx*8]
    xor esi, esi                ; low = 0
    mov edx, 100                ; high = 100
    call clamp
    mov [samples + rbx*8], rax
    inc ebx
    cmp ebx, COUNT
    jb .next

    mov rdi, samples
    mov esi, COUNT
    call print_array

    mov eax, 60
    xor edi, edi
    syscall

; clamp(x, low, high) -> x limited to low..high (signed)
clamp:
    mov rax, rdi
    cmp rax, rsi
    cmovl rax, rsi              ; below low  -> low
    cmp rax, rdx
    cmovg rax, rdx              ; above high -> high
    ret

; ---- print_array: print rsi unsigned quadwords starting at rdi on one line ----
print_array:
    push rbx
    push r12
    mov rbx, rdi            ; rbx = address of the next element
    mov r12, rsi            ; r12 = elements left
    sub rsp, 32             ; digit buffer, as in print_uint
.element:
    test r12, r12
    jz .finish
    mov rax, [rbx]          ; the number to convert
    lea rsi, [rsp + 31]
    mov byte [rsi], ' '     ; a space after each number ...
    cmp r12, 1
    jne .convert
    mov byte [rsi], 10      ; ... except the last one, which ends the line
.convert:
    mov ecx, 10
.digit:
    xor edx, edx
    div rcx
    add dl, '0'
    dec rsi
    mov [rsi], dl
    test rax, rax
    jnz .digit
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; length of digits + separator
    mov eax, 1
    mov edi, 1
    syscall
    add rbx, 8              ; next quadword
    dec r12
    jmp .element
.finish:
    add rsp, 32
    pop r12
    pop rbx
    ret
0 15 100 99 100 0 100

Counting without branches

setcc and adc/sbb are the cousins of cmov: they turn a condition into a number. Counting how many values exceed a threshold becomes straight-line code inside the loop:

nasm · try it yourself
section .data
readings:   dq 12, 55, 31, 78, 49, 90, 50, 3
COUNT       equ ($ - readings) / 8

section .text
global _start
_start:
    xor edi, edi                ; count = 0
    xor ecx, ecx
.loop:
    xor eax, eax                ; clear before the cmp (xor changes flags)
    cmp qword [readings + rcx*8], 50
    seta al                     ; al = 1 if reading > 50
    add rdi, rax                ; count += 0 or 1
    inc ecx
    cmp ecx, COUNT
    jb .loop
    call print_uint             ; 55, 78, 90

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
3

When cmov is (and isn't) better

Use cmov when… Prefer a branch when…
the condition is unpredictable (random data) the condition is almost always the same way
both values are cheap to compute one side is expensive (cmov computes both)
both values are safe to read one side is a memory load that might be invalid

The last point matters: cmovcc rax, [rbx] always reads [rbx], even when the condition is false. If rbx might be a null pointer, the load crashes regardless of the condition:

nasm · try it yourself
section .text
global _start
_start:
    xor ebx, ebx                ; rbx = 0: a "null pointer"
    mov eax, 7
    test rbx, rbx
    cmovnz rax, [rbx]           ; condition false, but the load still happens!
    mov rdi, rax
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
Program terminated with signal SIGSEGV (11)

Note: A 32-bit cmov writes its destination even when the condition is false, so cmovz eax, ecx always clears the upper half of rax — consistent with the 32-bit rule.

Test yourself

Which operand is NOT allowed as the source of cmov?

  1. a register
  2. a memory operand
  3. an immediate value
  4. a 64-bit register

After mov rax, rdi / cmp rdi, rsi, which instruction makes rax = max(rdi, rsi) for signed values?

  1. cmovg rax, rsi
  2. cmovl rax, rsi
  3. cmovb rax, rsi
  4. cmove rax, rsi

Why can cmovnz rax, [rbx] crash even if ZF = 1?

  1. cmov always performs its memory read
  2. ZF is ignored
  3. cmov needs aligned memory
  4. It can't