C8 · Lesson 9 · Branches and loops in assembly
Making decisions and repeating work
Left alone, a processor runs instructions one after another. Every decision and every loop in a program comes from one kind of instruction: a branch, which compares two registers and, if the comparison holds, loads the PC with a different address. An if is a branch that skips some instructions. A loop is a branch that jumps backward. This program finds the largest number in an array with both.
Play
- Step through and watch a0, the largest value found so far.
- Which instruction decides whether a0 changes? Which one sends the program round again?
- The array holds 27 twice. Does the second 27 change a0?
Predict
How many times does mv a0, t2 run?
Once. a0 starts at 14, and only the first 27 is bigger. ble t2, a0, loop branches past the mv whenever the new value is less than or equal to a0, so the second 27 leaves it alone, and so do −3, 8 and 19.
Name it
Branches, jumps and the shapes of control flow
Conditional branches compare two registers:
| Instruction | Branches when | Compares as |
|---|---|---|
beq a, b, L / bne | a = b / a ≠ b | bits |
blt a, b, L / bge | a < b / a ≥ b | signed |
bltu a, b, L / bgeu | a < b / a ≥ b | unsigned |
The assembler also accepts bgt, ble, beqz, bnez and bgtz, which it writes as these with the operands swapped or x0 filled in. Signed and unsigned comparisons differ when a top bit is 1: 0xFFFFFFFF is −1 signed but 4,294,967,295 unsigned.
Offsets. A branch stores the distance to its label, in bytes, relative to its own address, as a 13-bit signed number, so it reaches about 4 KiB either way. jal (and the pseudo-instruction j) jumps unconditionally with a 21-bit offset, about 1 MiB either way.
If and else. The branch tests the opposite condition and skips the code that shouldn't run:
bge a0, a1, else # if (a0 < a1) {
... # then part
j end # }
else: # else {
... # else part
end: # }
Loops. Test, body, jump back. A loop that tests at the bottom (like bnez t1, loop in lesson 1) always runs its body at least once; one that tests at the top can run it zero times.
Worked example
How many times does the body run here: li t0, 0, li t1, 10, then loop: (the body), addi t0, t0, 1, blt t0, t1, loop?
Show the solution
- Follow the counter. t0 runs from 0 to 9 during the body, and the loop ends when t0 reaches 10.
- Where the test sits. The test is at the bottom, so the body always runs at least once.
Answer ten times, with t0 = 0 to 9 during the body.
Worked example
Write if (a0 > a1) a2 = a0; else a2 = a1; with a single branch.
Show the solution
- Assume the else case.
mv a2, a1. - Skip the fix when it holds.
ble a0, a1, done. - Otherwise fix it.
mv a2, a0, thendone:.
Answer mv a2, a1, ble a0, a1, done, mv a2, a0, then done:.
Worked example
A loop with a test at the bottom runs a 3-instruction body 100 times, with an addi and a branch per pass and two instructions to set it up. How many instructions does it execute?
Show the solution
- Count the instructions.
- The overhead. The loop overhead, 200 instructions, is 40% of the work here.
- Unroll by four. Four copies of the body per pass and 25 passes cut the overhead to 50 instructions: at the cost of a longer program.
- Why compilers do it. Compilers unroll short loops for exactly this reason.
Answer 502 instructions, 200 of them loop overhead; unrolling by four brings it to 352.
Check
With n in a0, put 1 + 2 + … + n into a1.
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
a0 and a1 hold two signed numbers. Put the larger of them in a2.
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
A branch at address 0x080C jumps back to a label at 0x07F4. What offset, in bytes, is stored in the instruction?
Check
How many times does the body of this loop run?
li t0, 18
loop:
... # the body
addi t0, t0, -4
bgtz t0, loopCheck
t0 holds 0xFFFFFFFF and t1 holds 1. Which of these branches is taken?
Check
With n in a0, put n × (n − 1) into a1 using only additions in a loop (no mul).
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
A bottom-tested loop runs a 2-instruction body 100 times with 2 instructions of overhead per pass and 2 to set it up.
(a)
How many instructions does it execute?
(b)
And unrolled by 4?
Explain why
When an if statement becomes assembly, why does the branch test the opposite of the if's condition?
The processor falls through to the next instruction unless a branch jumps, and the instructions of the if's body come right after the test. So the body runs by default, and the branch's job is to jump over it when it shouldn't run, which is when the condition is false. That's why if (a < b) becomes bge a, b, skip: the branch is taken exactly when the body must be skipped. At the end of the body, a j jumps over any else part.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.