Quiz Entry - updated: 2026.12.01
Run the GCD assembly for gcd(12, 18). Which instructions execute, and what ends up in a2?
a2 ends up as 6. The loop runs twice: once taking the else path (b = 18 − 12 = 6) and once the if path (a = 12 − 6 = 6), after which a == b and the function returns.
Start: a2 = 12 (a), a3 = 18 (b).
| Step | Instruction | Effect | a2 |
a3 |
|---|---|---|---|---|
| 1 | beq a2, a3, .L1 |
12 ≠ 18, no jump | 12 | 18 |
| 2 | bgeu a3, a2, .L4 |
18 ≥ 12, jump to .L4 |
12 | 18 |
| 3 | sub a3, a3, a2 |
b = 18 − 12 | 12 | 6 |
| 4 | bne a3, a2, .L2 |
6 ≠ 12, jump to .L2 |
12 | 6 |
| 5 | bgeu a3, a2, .L4 |
6 ≥ 12 is false, no jump | 12 | 6 |
| 6 | sub a2, a2, a3 |
a = 12 − 6 | 6 | 6 |
| 7 | bne a3, a2, .L2 |
6 = 6, no jump | 6 | 6 |
| 8 | j .L1 |
jump to the exit | 6 | 6 |
| 9 | retw.n |
return with 6 in a2 |
6 | 6 |
The check: gcd(12, 18) = 6. Tracing a few instructions by hand like this is exactly what single-stepping with nexti in GDB shows you, one row per step, and it is the quickest way to check that you read the branches the right way round.
Tip: The trap in step 2 is reading bgeu a3, a2 as "a2 ≥ a3". The first register is on the left of the comparison.
Go deeper:
GDB manual — Source and Machine Code —
disassembleandx/ifor reading code at the machine level.