CS 14 -- Lab 3


In today's lab, we will do some assembly programming. In particular, we're going to use a program that simulates the MIPS architecture, known as SPIM. You're going to get set up with SPIM, make sure you can use it to run a program, and then do some assembly writing of your own.


Setup to use the MIPS simulator

In order to use the MIPS simulator, you must first logon to romulus.amherst.edu, which is a UNIX (Linux) machine. Bring up a shell (a terminal window). At the command line, type the following two lines, being sure to include the period that begins the filename on the second line:

~sfkaplan/public/cs14/init
source .cshrc

If you get an error message, ask for help. Note that you should not use these commands again, or else you might lose some of your work.


Try running a simple program

If all went well in the previous steps, you should be ready to go. Note that in the future, you simply need to login, bring up a terminal window, and begin work.

If you use the ``ls -l'' command, you will see that you now have three assembly files in your directory:

add-two-numbers.s
simple-conditional.s
find-max.s

Follow these steps to try running a program with SPIM and ensure that it works:

  1. First, run xemacs to examine the add-two-numbers.s, like so:

    xemacs add-two-numbers.s &

    Do you see how this program should work and what it should do?

  2. Now we're going to use SPIM itself. Note that this program is well documented in Appendix A of your book. Start SPIM with the following command, where the ampersand (&) is necessary to run the program in the background -- that is, SPIM continues to run while you are allowed to type more commands in your terminal window:

    xspim &
  3. You should see a new window appear that will have lots of information about the registers, the text (code) segment, the data segment, and some output on the bottom. There will also be some buttons in the middle of this window that we'll use to control SPIM.

  4. Begin by loading a program. Click on the load button. It will bring up a window, in which you can type add-two-numbers.s and press enter.

  5. You should see the instructions for that program appear in the middle of the window, with the first instruction highlighted. Notice that some of the instructions aren't quite what you expected! The li instruction has been replaced with ori. The registers are not given by symbolic name (like $s0) but by register number (like $16 for register number 16). Also note that data appears in hexidecimal, also known as base 16. We'll discuss why that representation has been chosen, and how to read it.

  6. Now click on step, so that we can run the program one instruction at a time, and see what happens. You will get a little window that asks for some more information -- you can just use the default values. Each time you click the step button within that little window, the current instruction will be executed, and the simulator will move to the next instruction. If the instruction was supposed to modify a register somehow, look at the register values and ensure that the value has changed as you expected.

  7. When you have stepped through this whole program, be sure that register $s0 has exactly the value that you thought it would. You can click the reload button, which will present (so long as you hold down the mouse button) a pull-down menu of one item (assembly program). Select that to reload and re-set the program. Note that you can run the whole program, without stepping, by clicking the run button.


Make a new, modified program

Try the same sequence of steps with the simple-conditional.s program. Do you see what it should do? Does it behave within xspim as you expected? Are there any surprises in how the original assembly is translated by the simulator?

Now try changing some things about the simple-conditional.s program. In your XEmacs window, do something to either the data or the branching instructions so that the other condition will be used. (That is, if the "then" was being taken, change the program so that the "else" is taken.) Load that program in SPIM and step through it, ensuring that the other branch is taken.


A problem to be solved

Now that you have a working simulator and are able to write different programs, you should open the find-max.s file in XEmacs, and complete the program that was started there.

Specifically, the code that exists in find-max.s creates an array. The register $s1 contains the beginning address of that array, and register $s2 contains the number of elements (where each element is one word) in the array. Numbers are put into the array, and then there is space in the file for you to write your own code. After that space is code that will end the program.

Your assignment: Fill in the space in find-max.s by writing MIPS assembly code that will search through the array and find the element with the maximum value. When the program ends, the register $s0 should contain that maximum value.


How to submit your work

We will be using the cs14-submit command to turn in programming work. Specifically, you should submit your completed find-max.s like so:

cs14-submit lab-3 find-max.s

This assignment is due Thursday, February 24th at 10:00 am.

Scott F. Kaplan
Last modified: Thu Feb 17 11:17:34 EST 2005