CS61C Lab 4

Assembly Practice

Background

Goals

This lab will give you practice running and debugging assembly programs using the SPIM interpreter.

Running Assembly programs

Assembly programs go in text files with a .s extention. The program must start with the label "main:" (similar to C) and end with a "jr $ra". $ra is set to the return address whenever a function is called, and main must return just like any other function.

In order to run your assembly programs, we will use SPIM, a MIPS R2000 interpreter. You can run spim by executing this on the command line:

$ spim

The ampersand causes the program to run in the background.

After starting spim, you can use the load command to load in your .s file. For example, if your MIPS program is named "foo.s", then typing the following will load your program.

  % load "foo.s"
To run your program, you can use the "run" command.

SPIM also functions as a debugger. You can set breakpoints, step through instructions one by one, view what are in your registers or in memory, as well as initialize values in registers before your program starts. For more information on using SPIM's debugger, type "help" at the prompt.

Exercises

Setup

Copy the contents of ~cs61c/labs/04 to a suitable location in your home directory.

$ mkdir ~/lab
$ cp -r ~cs61c/labs/04/ ~/lab

Exercise 1: Familiarizing yourself with SPIM

Answer the following questions about how to use SPIM.

  1. How do you load a file in SPIM?
  2. How do you set a breakpoint in SPIM using a label?
  3. After your program stops because of a breakpoint, how do you continue to execute your code?
  4. How can you find out the contents of a register?
  5. How do you change the value of a register?
  6. What is the difference between "reinitialize" and "reload"?
  7. How do you execute the previous command without having to retype it?
  8. What does -file flag do for SPIM?
Show your TA the answers to these questions.  

Exercise 2: A short MIPS program

Write a piece of MIPS code that, given values in $s0 and $s1, put into the $tk registers the following:

$t0 = $s0
$t1 = $s1
$t2 = $t0 + $t1
$t3 = $t1 + $t2
...
$t7 = $t5 + $t6

In other words, for each register from $t2 to $t7, it stores the sum of the previous two $t register values. The $s0 and $s1 registers contain the initial values.

Don't set the value of $s0 and $s1 in your code. Instead, learn how to set it manually with spim.

Checkoff

Save your code in a file called lab4_ex1.s and show it to your TA.  

Exercise 3: Debugging a MIPS program

Debug the loop in the program in lab4a.s. It is meant to copy integers from memory address $a0 to memory address $a1, until it reads a zero value. The number of integers copied (up to, but NOT including the zero value), should be stored in $v0.

Checkoff

Describe in a file lab4a.txt the bug(s) of the code.  
Create a file lab4a_ok.s that contains a bug fixed version of lab4a.s  

Exercise 4: Compiling from C to MIPS

The file lab4b.c is a C version of the program in the previous part of the lab. Compile this program into MIPS code using the command:

$ mips-gcc -S -O2 -fno-delayed-branch lab4b.c -o exercise3.s

The -O2 option (letter "O" and 2) turns on a level of optimization. The -S option generates assembly code. Don't worry about the delayed branch option for now; we will revisit this topic again when we talk about pipelining. The above command should generate assembly language output for the C code. Please note that you will not be able to run this code through SPIM.

Find the assembly code for the loop that copies sources values to destination values. Then, find where the source and dest pointers you see in lab4b.c are originally stored in the assembly file. Finally, explain how these pointers are manipulated through the loop.

Find the section of code in lab4b.s that corresponds to the copying loop and explain how each line is used in manipulating the pointer.  

Optional Exercise: Figuring out XSPIM

SPIM has a cousin. XSPIM is SPIM with a GUI interface. To run XSPIM, type the following:

$ xspim &

The & causes the program to run in the background, so that you can still use your console after you start running xspim. Although it has a GUI interface, it has some quirks that make it difficult to use at times. Try to reanswer all the questions in Exercise 1 for XSPIM using lab4a.s has the program you are working with.

Show your TA how to do the equivalent commands of SPIM you learned above in XSPIM.