A Quick Guide to SPIM
This document describes the Microsoft Windows version of the SPIM simulator for the MIPS assembly language. A full reference can be found in Appendix A of Computer Organization and Design by Hennessy and Patterson.
The SPIM simulator is a somewhat interactive environment for simulating the MIPS instruction set in assembly language. SPIM provides several windows that show what is happening in several areas of the simulated machine. They include:
Session: The “main” window that displays any messages from the SPIM simulator to the operator. Startup messages, error messages, and diagnostics show up here. This window is also used to show progress when single-stepping a program.
Console: This is the window that simulates the interface to the “user” of the program you are running. Text messages to the user are displayed here, and input from the user is entered here. The console is treated differently than the other windows and cannot be hidden behind them.
Registers: This window shows the current contents of all of the MIPS registers, including regular and floating-point registers, as well as special registers such as “LO” and “HI”. Note: Using this window causes the program to exit with a protection violation when running under Windows NT.
Data: This window displays the data segments of the memory for the current program. The most important portion of this window is simply labeled “Data” and includes user data (defined by .word, .space, .asciiz, etc.). The remaining portions show data used by the system.
Text: The text
window shows the program instructions in memory. This is simply the current assembly
language program loaded. Notice
that pseudoinstructions have been expanded out to MIPS instructions here. When this is done, the original
pseudoinstruction is appended to the right of the last corresponding instruction
as a comment.
The format of the data in the text window is as follows,
with an example:
[Instr. Location] Machine Code Assembly Code #original code
#comments
[0x00400068]
0x3c011001 lui $1, 4097
[0x0040006c]
0x00340821 addu
$1,$1,$20
[0x00400070]
0xac320000 sw $18,
0($1) #sw $s2,
num($s0)
#store number
In this example, the assembly instruction sw $s2,
num($0) has been translated into three machine instructions that accomplish the
same task (essentially, adding an offset to the memory access). Also, the symbolic register names $s0
and $s2 have been changed to their true register names.
1. Enter your program. SPIM does not include a program editor, so programs must be written and edited using a separate program. Any text editor will do. I have had good results with the Windows NotePad program. If you use some other editor, be sure to save the program as ASCII text, or you will get poor results.
2. Save your program. You must save your program every time you want to run it. SPIM does not know what changes you’ve made in your editor, but did not save. When you save your program, you should name is something with an extension of .S, for example, adding.S. The .S extension is traditionally used by UNIX assembly language programs and has been brought over to the SPIM simulator.
3. Clear out any garbage in SPIM. Before you load a new version of a program, you need to clear out any left over remnants of other programs in SPIM’s memory. To do this, select, in the SPIM simulator, File/Clear/All. This will erase the memory and registers to you will start with a clean slate. If you don’t do this, your program will not work.
4. Load your program. Once you’ve cleared out SPIM’s memory and registers, you can load your new program into memory. Choose File/Load from SPIM’s menus. Find your file using the dialog box and load it in.
5. Check the Session window for errors. If your program loaded fine, you should just see a welcome message and a message such as Read “C:\spim\adding.S” indicating that your program was read into memory. More likely, there may be syntax errors in your program. Any errors will be pointed out here. You’ll need to go back to your editor, fix them, save the program, clear SPIM’s memory space, and re-load.
6. Run your program. Choose Execute/Run from the SPIM menu bar. This will bring up a dialog box asking you for a starting address. The default is usually fine. Just click “Go”. Any I/O from your program will bring up the console window and show it there. Use the console window for any input to the program. You may have to select the console window in order to enter data into it.
7. Stopping endless loops. If your program is taking a long (possibly infinite) time, click the Interrupt button in the “Running” box. This will allow you to abort the program.
Debugging assembly language programs can be difficult. Here are a few features of SPIM that may help you.
1. Single-Stepping. If you choose Execute/Step from the menu bar, you can run your program one instruction at a time. The current instruction being executed is displayed in the Session window. Each time you click the Step button, one more instruction will be executed. Note: Things can get a bit confusing when single-stepping through console input from the user.
2. Breakpoints. You can instruct SPIM to run until a
certain instruction is encountered by setting a breakpoint. Basically, SPIM will stop executing when
the breakpoint instruction is executed so you can examine the registers and
single-step from this point if you wish. Select Execute/Breakpoints. A dialog box will appear for you to
enter a breakpoint into. Enter the
address of the instruction you want the breakpoint on and hit the Add
button. Now your breakpoint is
registered and you may run the program normally. When the breakpoint instruction is
encountered a breakpoint dialog box will pop up: choose Continue to keep on
going, or Abort to stop the program (note: you may choose Execute/Step after
Aborting).