Master the fundamental concepts of cpu design through this focused micro-challenge.
Branch instructions compare flags or registers and overwrite the program counter when a condition is true. Without branches, programs are straight-line calculators; with them, CPUs can loop, branch on error, and implement if/else.
| Opcode | Condition | PC update |
|---|---|---|
| JMP | always | PC = target |
| JEQ | Z == 1 | PC = target if zero |
| JGT | N xor V == 0 and Z == 0 | taken if greater (signed) |
cLoading…
Branches are cheap in your sequential emulator but expensive in pipelined hardware because the fetch stage may already pull wrong-path instructions. Later tasks add predictors and flushes.
For this exercise, you will implement JMP, JEQ, and JGT using the flag bits from your ALU. You will need correct PC writes to run a counting loop that terminates when a register hits zero.
Keep the relevant datasheet, ISA manual, or architecture textbook chapter open while you implement. When your output disagrees with the reference trace on the same program, the bug is usually a mis-decoded opcode, a stale register read, or a flag bit left unchanged after arithmetic.
For this exercise, you will use those habits while implementing the requirement in the starter code. Microarchitectural product names change across CPU generations, but the control ideas (fetch, bypass, cache lines, vector lanes) stay stable enough to debug from first principles.
Implement branch instructions for conditional and unconditional jumps.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github