Image goes here
SPIM Part 1
CptS 260 - Intro to Computer Architecture
Washington State University

MIPS and SPIM

Please use the links on the course resources page to obtain SPIM.\par

Typos to correct in the Britton book: on page 13 line 7 change the word "except" to the word "expect". In the gray box at the bottom of page 18 note that the separators on the second line are commas, not periods. On page 131 in the second case for the load immediate instruction should refer to Rd instead of Rt.

What is SPIM? SPIM is an assembler and simulator for the MIPS architecture. An assembler translates programs written instruction by instruction to machine code (ones and zeros). A simulator executes programs written for one machine on potentially a different machine.

Why is writing for an assembler better than writing machine code? (Even though the instructions are often in a one to one correspondence)

  • Comments
  • mnemonic opcodes
  • register names
  • automatic calculation of offsets for jumps
  • labels
  • some macro opcodes that expands to more than one instruction for example, in SPIM the abs $s0,$t8 instruction is actually a macro that expands to
       addu $s0, $zero, $t8
       bgez $t8, pos
       sub $s0, $zero, $t8
    pos:
    
    Note that the label pos: is on an otherwise-blank line. This is generally good practice for labels within code whereas labels for data often go on the line with the data declaraion.
Why write in assembler instead of a high-level language?
  • More predictable size and execution time
  • smaller size and faster execution
Why not?
  • Programming productivity
Perhaps the best of both is to use assembler for time and space critical code and a high-level language for most of the program. In this case, it is essential to follow the programming conventions of the high-level language in your assembler code. For the MIPS (and SPIM) this means, among other things, following the conventions about which registers are reserved for the assembler and kernel ($at, $k0, $k1), which registers are callee save and which are caller-save, etc.

When writing in assembly language it is wise to first code your programs in pseudocode--a high-level representation of the steps that the program should take. In pseudocode you can use if-then-elses and while loops but in assembler you have to code all the jumps explicitly. This can result in a big mess if you are not careful, and the way to be careful is to write pseudocode first. Refer to the Britton book, pages 14-16 for examples.

One important thing to know about both the if-then-else translation and the while loop translation is that you use the negation of the test in both cases when you are coding the jump at the beginning of the translation. Why?

(c) 2004-2006 Carl H. Hauser           E-mail questions or comments to Prof. Carl Hauser