\n"); } else { header("Content-type: text/html; charset=utf-8"); } ?> SPIM Assignment

Introduction

To help you become familiar with the MIPS machine and assembly languages, you are to write a function that computes the sum of the elements in a vector of integers. Your function name is to be addem. I am supplying a program called driver that will read in a set of numbers, call your function, and display the results.

You may use any text editor to prepare your program. Vim is available in the lab, and there is a Tiny Vim Tutorial for my CS-081 course that will provide you with enough information started with it. Alternatively, you could use Notepad or any other text editor of your choice to prepare your code.

Here’s an outline of your program:

.globl addem .text addem: # Your code goes here. jr $ra # Return to caller .end

The first two lines and the last one are assembler directives. The first line tells the assembler that addem is a global symbol; it’s needed so the driver program can call your code using this name. The .text directive tells the assembler that the information that follows is executable code. “Text” is the traditional Unix name for the code segment of a program. The last line is actually optional.

The jr $ra instruction is the standard MIPS instruction for returning from a subroutine. The MIPS jal (jump and link) instruction, which is used to call a subroutine, puts the return address in register 31, which has the symbolic name $ra.

The other information you need in order to write your code is how the driver program passes parameters to your program and how your program is to return the sum. In this case, the driver program puts the memory address of the vector to be summed in register $a0 (Register 4) and the number of words in the array in register $a1 (Register 5). Your function is to return the sum in register $v0 (Register 2).

Notes on Using The Simulator

When you first run SPIM, you may see a dialog box saying something about not being able to load the exceptions.s file. Select the option to browse to the file, and browse to C:\Program Files\PCSpim\exceptions.s You only have to do this once.

While you are looking at the Settings dialog box, be sure the options are set as follows: Save window positions, General registers in hexadecimal, Mapped I/O, Allow pseudo instructions, and Load exception file (as above) must all be ON (checked), and all other options must be OFF (unchecked). You can also get to the Settings dialog box from the simulator’s Simulator menu.

Download my driver.s file and save it in a directory that you set up for this assignment. Use your text editor to prepare your addem.s file in the same directory. Start SPIM, and use File->Open (or click the icon on the toolbar) to load either one of the two .s files. Then load the other one. If you are asked whether to reinitialize the simulator, click Yes for the first file and No for the second one. If you get any errors at this point, they should be in your file, and you will have to correct your code and try loading it again.

Once both files load successfully, use the F5 key to start the program running. You will be asked what starting address to use. The correct value (0x00400000) will be the default value, so you can just click OK. If all goes well, you will see a greeting in the simulator’s Console window, and a prompt for you to enter a number. You may need to Alt-Tab to the console window if it is hidden by the main simulator window. If the console window isn’t on the screen at all, something is probably wrong; it should appear automatically. But you can force it to appear by using the simulator’s Window menu.

Enter one number per line, and type Control-D to indicate that you are finished. Use both positive and negative values, and run the program several times with varying numbers of input lines to make sure it is robust. You cannot edit the numbers as you type them in, so any backspace or rubout keystrokes (or anything else that isn’t a decimal digit after the optional plus or minus sign at the beginning) will generate an error message and the number will be discarded.

Hopefully, your program won’t work correctly the first time. (You’ll learn more that way!) You can single step through the code using the F10 key, but you will probably want to let the program run full speed through my driver code until it gets to your function. (Also, I was unable to get the simulator to do character input when single-stepping.) To set a breakpoint, use the Simulator->Symbol table menu item to see what address your subroutine starts at. Then use Control-B to set a breakpoint there, and run the program using F5. Then you can single step through the instructions in your code, looking at the register values in the top pane and the contents of data memory in the third pane as you progress through the code.

If the simulator goes into an endless loop displaying “Exception” messages, use Control-C to stop it. Likewise, use Control-C if your function goes into an endless loop. In either case, you will need to reload the program files. Sometimes, you have no choice but to exit the whole simulator and start over.