CS61C Fall 2012 Lab 5: MIPS Assembly
This lab will give you practice running and debugging MIPS assembly programs using the MARS simulator.
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.
pull the lab to your working directory
$ cd ~/work
$ git pull ~cs61c/labs/fa12/05 master
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.
Show your TA the answers to these questions.
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.
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:
Show the answers to these questions to your TA for checkoff.