CS 61C (Fall 2007)

Lab Assignment 4


This lab will give you practice running and debugging assembly programs using the MARS simulator. You can also run MARS on your home computer by downloading the jar file from ~cs61c/bin/mars/mars-cs61c.jar on the instructional machines. While the original program comes from Missouri State University we have fixed a couple of bugs which have not yet been accepted by the main developers. You also need Java J2SE 1.5.0 (or later) SDK installed on your computer, which can be obtained from Sun.

Running Assembly programs

Assembly programs go in text files with a .s extention. The program must contain a label "main:" (similar to the main function in C programs) and end with a "addi $v0,$0,10" followed by a "syscall". Unlike normal functions which use "jr $ra" to return main is special and must transfer control back to the operating system when it is done rather than just returning.

For this lab we will be running our code in MARS, a MIPS simulator which provides a rich debugging GUI, rather than trying to run on a bare processor. In general, assembly programmers prefer this mode of development when possible as it is far easier to debug and work with the code.

You can run MARS in the lab, by running ssh -X cory, entering your password and then running mars-cs61c on cory. If you forget to run ssh -X cory to log into cory.cs.berkeley.edu before running MARS, the GUI will not display properly. This seems to be a problem with the java installation on nova.

To run MARS from home, you can download the file ~cs61c/bin/mars/mars-cs61c.jar, which is an executable JAR file. To run it, simply open a command line, navigate to the directory containing mars-cs61c.jar and run java -jar mars-cs61c.jar. Of course this will only work if you have java installed properly and on your path.

If you are coding your .s file on the instructional computers with no intent of running or debugging it, please use another (non-java) editor such as emacs, vi or pico so that we don't overwhelm the servers, which may run slowly, as a result of the inefficiency of java and the old age of the servers.

After starting mars, you can load your .s file using File->Open. You can edit your code by clicking the "Edit" tab and run or debug your program by clicking the "Execute" tab. In order to be able to execute your program, you must assemble the code first using Run->Assemble (F3).

To debug your assembly code in MARS 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.  You should spend some time before the lab getting familiar with MARS if possible.



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

Exercise 1: Familiarizing yourself with MARS

Load lab4_ex1.s into MARS and assemble the code. Assume that fib[0] = 0; fib[1] = 1; fib[n] = fib[n-1] + fib[n-2]
Use Help (icon with the question mark) in order to answer the following questions about how to use MARS.

  1. What do the .data, .word, .text directives mean (i.e., what do you put in each section)?
  2. How do you set a breakpoint in MARS? Set breakpoint on line 15 and show this to your TA when you have answered all of these questions.
  3. After your program stops because of a breakpoint, how do you continue to execute your code? How do you step through your code?
  4. How can you find out the contents of a register? How do you modify the value of a register.
  5. At what address is n stored in memory? Calculate the 13th fib number by modifying this memory location.
  6. line 16 and 18 use syscall instruction. What is it and how do you use it? (hint: syscall inside Help!))
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 $t? 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 MARS.

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

Exercise 3: Debugging a MIPS program

Debug the loop in the program in lab4_ex3.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.

Describe in a file lab4_ex3.txt the bug(s) of the code. Create a file lab4_ex3_ok.s that contains a bug fixed version of lab4_ex3.s. Show these both to your TA.

Exercise 4: Compiling from C to MIPS

The file lab4_ex4.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 lab4_ex4.c -o lab4_ex4.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 MARS.

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 lab4_ex4.c are originally stored in the assembly file. Finally, explain how these pointers are manipulated through the loop.

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