Computer Science 2500
Computer Organization

Rensselaer Polytechnic Institute
Spring 2009


Lab 3: MIPS Programming with SPIM
Due: 11:59 PM, Wednesday, February 11, 2009


This lab assignment is designed to get you started with MIPS programming using SPIM. The amount of coding is relatively small but you should expect to spend some time getting familiar with SPIM, and perhaps installing an appropriate version on your own computer.

Getting Started

Copy one or more of the class examples and make sure you can run them in the SPIM environment of your choice, and on the RPI CS Lab FreeBSD systems.

In order to run on the CS Lab FreeBSD systems, you will need to set up your account to find my installation of SPIM, as demonstrated in class, by adding the path /cs/terescoj/shared/cs2500/bin to your search path. The SPIM executable found there works only on FreeBSD, so be sure to connect to freebsd.remote.cs.rpi.edu.

Programming 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.

Programming 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 opcode).

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.

What To Turn In

Submit a single tar file called lab3.tar that will extract its contents into a directory spim. The tar file should include your two assembly files: swapelements.s and sumarray.s.

Note: During grading, we 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 Lab FreeBSD systems, as that is where we 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. When your tar file is ready to go, you may submit it from any CSLab Unix system with the command:

submit comporg lab3 lab3.tar