CS 61C (Fall 2011)
Lab Assignment 3
This lab will give you practice running and debugging assembly programs using the MARS simulator.
Assembly programs go in text files with a
.s extention. The program must contain a label "
main:" (similar to the
in C programs) and end with a
addi $v0,$0,10" followed by a "
normal functions which use "
jr $ra" to return
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 typing the
To run the program remotely, you may either run via an instructional server (such as nova.cs.berkeley.edu, but not one of the macs), or through a local installation (recommended)
. When on an instructional server,
you will need to follow the usual steps of running an X-Server (like XMing), and enabling X11 tunneling. The
mars command will run the program.
To run a local installation, you can download the file
~cs61c/bin/Mars_4_1.jar, which is an executable
JAR file. To run it, simply open a command line, navigate to the directory containing
java -jar Mars_4_1.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 (non-mac) 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
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
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. .
Copy the contents of ~cs61c/labs/03 to a suitable location in your home directory.
$ mkdir ~/lab $ cp -R ~cs61c/labs/03/ ~/lab
Anytime you are working with MIPS the green sheet (in the front of your textbook) is a very helpful resource, if you did not bring it with you, you can get it here.
Load lab3_ex1.s into MARS and assemble the code (to assemble simply press the button with the wrench and screwdriver on the ribbon along the top). Assume that fib = 0; fib = 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.
.textdirectives mean (i.e., what do you put in each section)?
nstored in memory? Calculate the 13th fib number by modifying this memory location.
syscallinstruction. What is it and how do you use it? (hint: syscall inside Help!))
Write a piece of MIPS code that, given values in
$s1, put into
$t? registers the following:
$t0 = $s0 $t1 = $s1 $t2 = $t0 + $t1 $t3 = $t1 + $t2 ... $t7 = $t5 + $t6
In other words, for each register from
$t7, it stores the sum of
the previous two
$t? register values. The
$s1 registers contain
the initial values.
Don't set the value of
$s1 in your code. Instead, learn how to
set it manually with MARS.
lab3_ex2.sand show it to your TA.
lab03_ex3.c is a C version of the program in the previous part of
Compile this program into MIPS code using the command:
$ mips-gcc -S -O2 -mno-explicit-relocs lab03_ex3.c -o lab03_ex3.s
-O2 option (letter "O" and 2) turns on a level of optimization. The
-S option generates assembly code. Don't worry about the mno explicit relocs, it just keeps the output cleaner. 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
dest pointers you see in
lab03_ex3.c are originally stored in the assembly file. Finally, explain how these pointers are manipulated through the loop.
Find the section of code in lab03_ex3.s that corresponds to the copying loop and explain how each line is used in manipulating the pointer.