MIPS pipeline
Write a MIPS assembly program, or pick an example, and watch it go through the 5-stage pipeline cycle by cycle: where each bubble goes, where each forwarded value comes from and what each branch costs. The program is run by SPIM, the core of QtSpim, so loops go round as many times as they really do. It runs in your browser, on your phone too.
How to use it
- Write your program in the editor, with its
main:label, or pick an example: the cafeteria till from the theory, the same one reordered, a loop adding an array or a subroutine withjalandjr. - Press Run with SPIM. The simulator runs the program from
mainand places the instructions in the pipeline in the order they were executed (pseudo-instructions such asliorlaappear as the real instructions they turn into). - Choose forwarding on or off and what to do with branches: predict not taken or stall. The terminal command changes with your choices.
- Press Step to move one cycle, or Run to watch it all. At the top you see which instruction is in each stage and an explanation of what happens in that cycle; below, the multi-cycle diagram fills in.
- Tap an instruction in the diagram to see why it waits and where each operand comes from. Share copies a link with your program.
How to read the diagram
Each row is an executed instruction and each column a cycle. The stages have their own colour: IF (fetch), ID (decode and read registers), EX (ALU), MEM (data memory) and WB (write the result back). Striped cells are bubbles: the instruction waits in ID. Coral arrows are forwarding (the value goes from EX/MEM or MEM/WB to the ALU input) and dashed violet ones, a register written in the 1st half of the cycle and read in the 2nd. A row with a × is an instruction fetched behind a branch and discarded.
The maths: cycles = 4 + instructions + data bubbles + cycles lost to branches, and CPI = cycles ÷ instructions. The comparison repeats the same trace with and without forwarding and with both ways of handling branches, like the parts of the exam question.
The rules it follows
- It is the model from the course notes: five one-cycle stages and a register file that writes in the 1st half of the cycle and reads in the 2nd.
- Without forwarding, a reader has its ID in the cycle of the writer’s WB, or later.
- With forwarding, an ALU result is ready at the end of its EX and a
lw’s at the end of its MEM: alwfollowed by its use costs one bubble even with forwarding. - Conditional branches and
jrcompare or read in ID, so they need their data one cycle before the ALU would.j,jalandjralways lose one cycle. - No delayed branches, like QtSpim by default. At most the first 80 executed instructions are drawn.
For the exam
The pipelining question in the exam gives a code fragment and asks for the cycles with and without forwarding, a reordering that removes bubbles and the speedup. Type that fragment here and compare your paper diagram with the simulator’s; the challenge below gives you new fragments every time. The full theory, with the cafeteria till, is in the Pipelining topic.
