Computer Science 220
Assembly Language & Computer Architecture

Fall 2011, Siena College

Lab 11: MIPS Programming with SPIM
Due: the start of your next lab meeting

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

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:

Four 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 is available on olsen (the course home page has instructions on how to set up your account to run it). PCspim is available on the Windows systems in our lab. If you install something on your own computer, go with QtSpim, as that is the newest version.

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:
~jteresco/shared/cs220/examples/spim-simple/simple.s

Parts of that are familiar: MIPS assembly language 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, PCspim has a much friendlier (even if a bit old fashioned) interface for tracing through and debugging programs. Run simple.s using PCspim and experiment with the interface.

Another simple example:

See Example:
~jteresco/shared/cs220/examples/spim-simple/hello.s

A few things to note here:

One more simple example:

See Example:
~jteresco/shared/cs220/examples/spim-simple/powers.s

This one allocates space for an array of integer values and fills it with the first few powers of 2.

Notes on this one:

In Lab: Encountering Errors in SPIM

Unless you're the best beginning MIPS programmer in history, you will probably make some errors when you develop your code.

Consider these three examples:

See Example:
~jteresco/shared/cs220/examples/spim-errors/syntax.s

See Example:
~jteresco/shared/cs220/examples/spim-errors/illegal.s

See Example:
~jteresco/shared/cs220/examples/spim-errors/pointer.s

The first includes a :syntax" error that will be caught when we run the simulator (we left off the required register on a jr instruction). The second uses an illegal operand on an addi instruction (the third operand needs to be a constant that fits in 16 bits). The last attempts to access values from memory as words but attempts to use some addresses that are not word-aligned (that is, multiples of 4).

Take a look at the errors generated when you run these examples in spim. Hopefully, you will have a better idea what to look for when you see similar messages from your own programs.

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
endar:
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 the start of your next lab meeting, submit documented MIPS assembly source code for your two programs. All necessary files should be submitted as email attachments to jteresco AT siena.edu.

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 my 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 olsen, 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.