Goals
This lab will give you practice running and debugging MIPS assembly programs using the MARS simulator.
Running Assembly programs
Assembly programs go in text files with a .s extension. The program must contain a label main:
(similar to the main function in C programs) and end with a li $v0,10
followed by a syscall
. Unlike normal functions which use jr $ra
to return, main:
is special in that when it is done, it must transfer control back to the operating system via a system call (consider: what would happen if main:
just did jr $ra
?)
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 bare hardware. Assembly programmers often prefer this mode of development, as it is far easier to debug and work with the code interactively.
To run the program remotely, you may either run via an instructional server (but not one of the Macs), or through a local installation. You can invoke MARS with the following command:
$ mars &
To run MARS on your own local computer, you can download Mars.jar at this link.
If you are coding your .s file on the (non-Mac) instructional computers with no intent of running it, please don't use the MARS editor. Use another (non-graphical) editor such as emacs or vi so that we don't overwhelm these 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. Spend some time before the lab getting familiar with MARS.
Exercises
Setup
To obtain the lab05 files, pull from the git repo at ~cs61c/labs/fa13/05
. For example:
$ mkdir ~/lab05 $ cd ~/lab05 $ git init $ git pull ~cs61c/labs/fa13/05 master
Exercise 1: Familiarizing Yourself With MARS
Load lab05_ex1.s into MARS and assemble the code.
Use Help (the icon with the question mark) in order to answer the following questions about how to use MARS.
- What do the
.data
,.word
,.text
directives mean (i.e., what do you put in each section)? - How do you set a breakpoint in MARS?
- After your program stops because of a breakpoint, how do you continue to execute your code?
- How do you step through your code?
- How can you find out the contents of a register?
- How do you modify the value of a register?
- What does this program do?
- At what address is
n
stored in memory? Run the program for n=13 by modifying this memory location. Do not modify the code! - Notice the uses of the
syscall
instruction. What is it and how do you use it? (hint: look up syscall inside Help!))
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 XOR 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, practice how to set it manually with MARS.
Save your code in a file such as lab05_ex2.s
Exercise 3: Compiling from C to MIPS
You will be compiling the C file lab05_ex3.c
into MIPS. If you are running things remotely, make sure you are on a hive machine.
Compile this program into MIPS code using the command:
$ mips-gcc -S -O2 -fno-delayed-branch lab05_ex3.c -o lab05_ex3.s
The -O2
option (letter "O" and 2) turns on a level of optimization. The -S
option generates assembly code. The above command should generate assembly 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
lab05_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 lab05_ex3.s
that corresponds to the copying loop and explain how each line is used in manipulating the pointer.
Exercise 4: Reverse Assembling
Write the mips instructions that get assembled into the following machine code.
main: 0x8c880000 0x34090001 0x01094020 0xaca80000 0x2402000a - addiu $v0,$0,10 0x0000000c - syscall
Manually set the values of the necessary registers so that the program runs without errors.