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
cmp rax, rbxcomputes a subtraction it never stores.jgtakes the jump only when the first was greater.- The fall-through path handles the other case: a max.
Keywords and builtins used here
cmpglobaljgmovraxrbxrdisectionsyscall
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.
Step 1 of 4 in Control flow, step 11 of 31 in Language basics.