CS61C Lab 4

Assembly Practice

Background

Goals

This lab will give you practice running assembly programs using the xspim interpreter.

Running Assembly programs

Assembly programs go in text files with a .s extention. The program must start with the label "main:" (just like in 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.

xspim is a version of spim that runs on the X windows graphical user interface, as opposed to spim's console interface. You can run spim remotely, but you will have to refer to the spim documentation to find the equivalent commands. You can run xspim by executing this on the command line:

$ xspim &

The ampersand causes the program to run in the background.

After starting xspim, use the load button to load in your .s file. The set buttons can be used to set the register values, and the program can then be executed using the run or step button. The register values will change in the top frame.

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: 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 xspim.

Checkoff

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

Exercise 2: 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 3: 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

The -O2 option turns on 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. Find the assembly code for the loop that copies sources values to destination values. Then, for the registers $a0, $a1, $v0, and $v1 from part 2, determine what registers gcc used to store the corresponding value. (For example, $a0 was used to store the source address of integers to be copied. What register is used for this purpose in the gcc output?)

Find the section of code in lab4b.s that corresponds to the copying loop. Comment each line of that part of the code in lab4b.s.