typestar

cmp and jumps in Assembly

cmp sets the flags; the j-family branches on them.

; cmp sets flags; the j-family reads them. This picks a maximum.
        global _start

        section .text
_start:
        mov rax, 23
        mov rbx, 61

        cmp rax, rbx
        jg .keep_a              ; jump if rax > rbx
        mov rax, rbx            ; otherwise take rbx
.keep_a:
        mov rdi, rax            ; max(23, 61) = 61
        mov rax, 60             ; exit code 61
        syscall

How it works

  1. cmp rax, rbx computes a subtraction it never stores.
  2. jg takes the jump only when the first was greater.
  3. The fall-through path handles the other case: a max.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
327
Tokens
38
Three-star pace
60 tpm

At the three-star pace of 60 tokens a minute, this run takes about 38 seconds.

Type this snippet

Step 1 of 4 in Control flow, step 11 of 31 in Language basics.

← Previous Next →