Learning path

Full curriculum

Full curriculum

Unit content

Machine-level control flow

At source level, conditions and loops choose which statements execute. At machine level, that choice becomes arithmetic on the instruction stream.

Comparisons and branches

A processor can compare values or compute condition information and then execute a conditional branch that changes the program counter only when the condition is satisfied.

compare x, 0
branch-if-zero done
...
done:

An unconditional jump always transfers control to another instruction address.

Loops are repeated branches

A loop can be lowered into ordinary instructions that update loop state, test a condition and branch back to an earlier address.

loop:
    work
    update
    if condition: jump loop

The processor does not need a source-language concept of if, while or for; these structures are implemented from instruction sequencing and branches.

Control flow changes execution order

Branches are also the reason a processor cannot always assume that the instruction physically following the current one will execute next. That uncertainty becomes important in pipelined processors and branch prediction.