TA Corner

Up

Whats New
Class Info
Calendar
Lecture
Laboratory
Homework
TA Corner
FAQ

Help on the Lab programs

  1. Writing a program in SPIM    General hints and helps
  2. Move to coprocessor             Reverse syntax for this item
  3. Data Items                                No error for faulty input for 0x items
  4. Ending a SPIM program        Need syscall to exit

Writing a program in SPIM

Writing the Program

Use Notepad/Wordpad to write your program. PCSPIM requires one blank line after your last line of code. So, ensure that you press the carriage return key at least once at the end of your program. Save the program with a ".s" extension. For example, Program1.s.

Using the Simulator

Double click on the PCSpim icon on your desktop. When the simulator is started, you should see 4 windows titled "Messages", "Data Segment", "Text Segment", and "Registers". If you don't see them, click on the Window menu and select Tile.

Loading a Program

Select the Open file icon from the toolbar. Alternatively, you can select from the menu bar: File->Open. A file open dialog box will appear for you to select the appropriate assembly file. Select the appropriate assembly file and click on the button labeled Open in the dialog box. If simulator settings are not correct for the file, and it fails to load, PCSpim will provide you an opportunity to change simulator settings and automatically reload the file.

If you change your mind, click on the button labeled Cancel, and PCSpim removes the dialog box. When you load an assembly file, PCSpim removes dialog box, then loads your program and redraws the screen to display its instructions and data. If you have not done so, change the view of the four display windows to a tiled format by selecting from the menu bar: Windows->Tile. You should be able to see the program in the Text segment window display.

Each instruction in the Text segment window display is shown on a line that looks like:

      [0x00400000] 0x8fa40000 lw $4, 0 ($29); 89: lw $a0, 0($sp) 

The first number on the line, in square brackets, is the hexadecimal memory address of the instruction. The second number is the instruction's numerical encoding, again displayed as a hexadecimal number. The third item is the instruction's mnemonic description. Everything following the semicolon is the actual line from your assembly file that produced the instruction. The number 89 is the line number in that file. Sometimes nothing is on the line after the semicolon. This means that the instruction was produced by SPIM as part of translating a pseudoinstruction.

Running a Program

Click on the Go button in the toolbar. Alternatively, you can select Simulator->Go from the menu bar. Your program will begin execution. If you want to stop the execution of your program, select Simulator->Break from the menu bar. Alternatively, you can type Control-C when PCSpim application window is in focus. A dialog box will appear and ask if you want to continue execution. Select No to break the execution. Before doing anything, you can look at memory and registers contents in the Register display window to find out what your program was doing. When you understand what happened, you can either continue the program by selecting Simulator->Continue or stop your program by selecting Simulator-> Break from the menu bar.

If your program reads or writes from the terminal, PCSpim pops up another window called the console. All characters that your program writes appear on the console, and everything that you type as input to your program should be typed in this window.

Debugging a Program

SPIM has two features that help debug your program. The first is single-stepping, which allows you to run your program an instruction at a time. Select Simulator->Single_Step to execute only one instruction. Alternatively, you can press the F10 function key to single step. Each time you step through a program, PCSpim will execute the next instruction in your program, update the display, and return control to you. You can also choose the number of instructions in your program to step by selecting Simulator->Multiple_Step instead of single stepping through your program. A dialog box will appear and ask you the number of instructions to step.

If your program runs for a long time before the bug arises, a better alternative is to use a breakpoint, which tells PCSpim to stop your program immediately before it executes a particular instruction. Select Simulator->Breakpoints from the menu bar. The PCSpim program pops up a dialog box window with two boxes. The top box is for you to enter breakpoint address and the second box is a list of active breakpoints. Type in the first box the address of the instruction at which you want to stop. Or, if the instruction has a global label, you can just type the name of the label. Labeled breakpoints are a particularly convenient way to stop at the first instruction of a procedure. To actually set the breakpoint, and click on the button labeled Add. When you are done adding breakpoints, click on the button labeled Close. You can then run your program. When the simulator is about to execute the breakpointed instruction, PCSpim pops up a dialog box with the instruction's address and asks if you want to continue the execution. The Yes button continues running your program and the No button stops your program. If you want to delete a breakpoint, you can select Simulator-> Breakpoints from the menu bar, click on the address in the dialog box, and click on the button labeled Remove.

Fixing Bugs in a Program

Single-stepping and setting breakpoints will probably help you find a bug in your program quickly. To fix a bug, go back to the editor that you used to create your program and change your source file. After you have made the changes to your source file, simply reload it into PCSpim for Windows by choosing Simulator-> Reload<filename> from the menu bar. This causes PCSpim to clear its memory and registers and return the processor to the state it was in when PCSpim first started. Once the simulator has reinitialized itself, it will reload your recently modified file.

 

Move to coprocessor 

2002-02-10

Spim has a reverse syntax for the move to coprocessor.  The symptoms are a syntax error and the program will not load. The fix is to reverse the order of the operands. A sample of an instruction that will assemble is:

                 mtc1 $a0, $f12                # Move to co-processor

The instruction copies the contents of CPU register $a0 to coprocessor 1, register $f12.  The order is the same as the store to memory instructions.  The documentation in this case is incorrect at least for some versions of SPIM.  To check out the version of SPIM that you are using, you can write a simple two or three line program to see if it assembles and that it functions. 

Data Items

Spim continues to display some C like oddities.  It has been noticed that if you  declare a data item like

    .word    0x12345

that the x must be lower case. If it is upper case, the word will assemble as all zeros with no error message.

Ending a SPIM program

The newest version of SPIM requires a syscall to accomplish an orderly return back to the SPIM simulator.  Previous versions allowed the program to run off the end with no unusual behavior.

Use the following sequence to end your program

    lw    $v0, 10       # Setup register for exit
    syscall                # Exit to SPIM simulator

The sequence is similar to the "call exit" used by some other languages.