Computer Science 220
Assembly Language & Computer Architecture

Fall 2010, Siena College

Lab 3: MIPS Programming with SPIM
Due: 10:00 AM, Friday, October 8, 2010

This lab assignment is designed to get you started doing your own MIPS programming using SPIM. The amount of coding is relatively small but you should expect to spend some time getting familiar with SPIM (which you should be able to do during the lab meeting), and perhaps some time installing an appropriate version on your own computer if you wish.

You may work individually or with a partner on this lab.


SPIM is a free and open source software simulator of the MIPS architecture that we will use to execute MIPS programs. It was developed and is maintained by James Larus. You can learn more and download your own copy at SPIM's web page.

SPIM's capabilities include:

Three SPIM versions are available:

I will grade submissions with spim, but you are welcome to use any version you wish when developing MIPS programs.

spim and xspim are available on the Linux systems in our labs.

In Lab: Running Complete MIPS Assembly Programs

A complete program consists of several parts, including the MIPS assembly code we have been considering in our class examples. Consider a very simple MIPS program.

See Example:

Parts of that are familiar: MIPS instructions. But there are several other things to notice that allow us to take a set of MIPS instructions and turn them into a complete MIPS program, capable of being executed in SPIM.

Here are some things to notice about this example:

To run this at the command line:

spim -file simple.s

This should print our answer.

We can learn much more about the execution of the program running SPIM in interactive mode.

Run spim without any command-line parameters to get the (spim) prompt, and type help to see the options available. Load the program into the simulator, then step through the program, examining the contents of the registers as you go.

Some things to notice when stepping through our program:

While the text-only spim simulator is fine for running a program to obtain a result, xspim has a much friendlier (even if a bit old fashioned) interface for tracing through and debugging programs. Run simple.s using xspim and experiment with the interface.

Another simple example:

See Example:

A few things to note here:

Finally, we look at a complete version of the factorial program you saw/will see in class. (Tuesday lab group may wish to skip this example until after seeing it in Thursday's lecture.)

See Example:

Programming Tasks

Task 1

First, write a MIPS assembly language program sumarray.s to sum the elements of an array.

To declare the array, do something like this:

ar:   .word 5, 17, -3, 22, 120, -1	# a six-element array

This will allocate space for 6 word-sized values and will assign the ar label to refer to the address of the first element.

Fill your array with an assortment of initial values, and declare a sum variable, using .space 4 as in the simple.s example. The simplest way to loop through an array is to use a pointer and stop when it runs off the end of the array. You can do that by declaring a label right after the array, and then initializing a register to point to the start of the array (i.e. in the program, initialize the register with the array address) and looping until your pointer register equals the value in the end-of-array register (which is the address just past the array).

ar:	.word 5, 17, -3, 22, 120, -1	# a six-element array
sum: .space 4

At the end, print out the value of the sum variable.

Task 2

Second, write a MIPS assembly language program swapelements.s that will swap the contents of two elements of an array, given the array and the indices of the elements to be swapped.

For the indices, use two more integer variables declared in the data section with .word. (Warning: you can't use `j' as a label/variable name, since j is an instruction name).

Print all of the elements of your array both before and after the swap. This will involve a loop much like the one you wrote for the first task of this lab.

Submission and Evaluation

This lab is graded out of 35 points.

By 10:00 AM, Friday, October 8, 2010, submit documented MIPS assembly source code for your two programs. All necessary files should be submitted by email to jteresco AT

Grading Breakdown

summarray.s correctness 12 points
swapelements.s correctness 17 points
Documentation 6 points

Note: During grading, I will replace your array's contents with our own, so be sure your programs work with a variety of array sizes and contents. Also, make sure that your programs work in SPIM on the CS Linux systems, as that is where I will test your programs.

Appropriate documentation is especially important in assembly language programs (and make sure your name is in each of the files). Equivalent high-level language code often makes good comments. Be sure to describe your register and memory usage clearly in the comments.

Please do not include any additional files, such as emacs backup files.