CS 14 -- Lab 4

Note that the lab number corresponds to the week number in the semester!


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. Here, we will describe how to do that while in the Sun Lab (SMudd 112); connecting from home is another topic for which there will be posted instructions. Follow these steps to get yourself working in the Sun Lab for the first time:

  1. Move the mouse or hit a key to bring up the screen. It will ask you to select a machine to which to login. Choose romulus.amherst.edu by double-clicking.
  2. After a few seconds, you will see a login screen. Login using your NT username and password. It will take a few moments, but you'll eventually see a graphical environment appear.
  3. On the bottom of the screen, you will see an icon that looks like a terminal screen. Click once on that icon, and you should get a terminal window where you can type commands.
  4. Type the following command:
    chsh
    When prompted, enter your password again. At the next prompt, enter the following:
    /bin/tcsh
    There are two cases for what to do next:
    1. If the chsh command ended by responding, ``Shell changed'', then you must logout completely and log back in. Go to the paw icon (in the lower left hand corner) and click it once to get a menu. The bottom menu item is Log out, so choose that. Once you've logged out, log back in again.
    2. If the chsh command ended by responding, ``Shell not changed'', then you need only to type:
      exit
      That will close your terminal window.
  5. Click on the terminal icon to get a new terminal window. You may see a different command line prompt due to the change in shell. (If you know what a ``shell'' is, don't worry about it too much. It is merely the command line program itself.)
  6. At the command line, type the following two lines, being sure to include the period that begins the filename on the second line:
    /mnt/user1/sfkaplan/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 two assembly files in your directory:

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 simple-conditional.s, like so:
    xemacs simple-conditional.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 simple-conditional.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

Now that you've actually run a MIPS program, you need to change some things about that program. In your emacs 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 emacs, 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.


Finishing the lab

  • In the lower left-hand corner of the screen, there should be a paw print (cute, eh?). Click once on it to get a menu. The last item on the menu should be Log out''. Select that menu item, and click Yes to confirm that you want to log out.
  • Be sure, when done in the Sun Lab, to logout from the terminal. You can do this by clicking the Exit button in the center control bar.

    We will be using the submit command to turn in programming work. Note that the submit command is not yet set up -- I will configure it soon and provide instructions on its use so that you can submit your work.


    This assignment is due Monday, February 25th at 11:59 pm.

    Scott F. Kaplan
    Last modified: Tue Feb 19 14:31:37 EST 2002