CS61C Fall 2012 Lab 5: MIPS Assembly

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: 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 computer, you can download Mars.jar at this link. To run it, simply open a command line, navigate to the directory containing Mars.jar and run java -jar Mars.jar . Of course, this will only work if you have java installed properly.

 

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 aging 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

pull the lab to your working directory

 

$ cd ~/work

$ git pull ~cs61c/labs/fa12/05 master

Exercise 1: Familiarizing yourself with MARS

Load lab05_ex1.s into MARS and assemble the code. This assembly program calculates the nth Fibonacci number, where n is a variable in memory.  (Recall: if F(n) gives the nth Fibonacci number, then F(0) = 0, F(1) = 1, and F(n) = F(n-1) + F(n-2) for n > 1.)

Use Help (icon with the question mark) in order to answer the following questions about how to use MARS.

 

  1. What do the .data and .text directives mean (i.e., what do you put in each section)?  What does the .word directive mean?
  2. How do you set a breakpoint in MARS? Set a 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 resume execution? How do you single-step through your code?
  4. How can you find out the contents of a register? How do you modify the contents of a register?
  5. At what address is n stored in memory? Calculate the 13th Fibonacci number by modifying this memory location.
  6. Lines 24 and 26 use a syscall instruction. What is it and how do you use it? (Hint: look up Syscalls inside MARS Help!)

 

Show your TA the answers to these questions.

Exercise 2: 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. Show your TA the code and explain what the code does.

Exercise 3: Compiling from C to MIPS

Next week, we will talk about the process of compilation.

 

The file lab05_ex3.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 lab05_ex3.c

 

The -O2 option (that’s the letter O, not the number 0) instructs the compiler to turn on most optimizations.  The -S option instructs it to generates assembly code, rather than object code. The above command will generate assembly language output for the C code in lab05_ex3.c, writing the output to lab05_ex3.s.  Note that you will not be able to run this code in MARS.

 

Examine at lab05_ex3.s and answer the following questions:

 

  1. Into which registers are the base addresses of the source and dest arrays loaded?
  2. How are these pointers manipulated in the assembly corresponding to the for loop?

 

Show the answers to these questions to your TA for checkoff.