Topic 2 · Class activity 7 · Week 5 · Wednesday

Your First Running Program

Hand-write the MIPS, run it in SPIM, name the three code-generation decisions you just made, then make the compiler produce it.

Format pairs, at a laptopTime 55 minNeed SPIM or QtSPIM installed

The point

Get one program all the way to a running executable. Nothing in this course feels real until that happens, and the three problems of code generation stop being a list the moment you have to solve them.

Do this

  1. By hand first. Take three TAC instructions —
    t0 = 2 + 3
    total = t0
    PRINT total
    — and write the MIPS yourself, assuming total lives at 0($sp). You need li, add, sw, lw, and two syscalls.
  2. Assemble and run YOUR version. Wrap it in a .text / .globl main / main: skeleton and run spim -file yours.s. Fix it until it prints 5.
  3. Name the decisions you just made. For each of Dragon's three problems — instruction selection, register allocation, evaluation order — write one sentence saying what you chose and what the alternative was.
  4. Now implement the code generator for those three TAC opcodes.
  5. Diff. Compare the compiler's output with your hand-written version. Where the compiler emitted more instructions, say why, and whether the extras are actually necessary.
  6. Finish the phase and run every test in tests/.
The two syscalls you need
    li   $v0, 1        # print integer, value in $a0
    syscall

    li   $v0, 10       # exit
    syscall

Deliverable

Your hand-written .s, the compiler's, a screenshot of both running in SPIM, your three sentences from step 3, and your answer to step 5. This is Lab Questions 9 and 10.