Goals
MIPS compilers also generate a number of assembler directives that spim cannot process. These directives usually can be safely ignored. Earlier versions of spim (before 7.0) implemented the MIPS-I instruction set used on the MIPS R2000/R3000 computers. This architecture is obsolete (though, never surpassed for its simplicity and elegance). In the Mars Messages text area will be highighted and the corresponding erroneous instruction will be selected in the text editor. In addition, you can click on any error message in the Mars Messages text area to select the corresponding erroneous instruction in the text editor. The first feature does not select in every situation (such as when.
This lab will give you practice running and debugging assembly programs using the MARS simulator, as well as writing MIPS functions.
Setup
Copy the lab files with
Intro to Assembly and MARS
The following exercises use a MIPS simulator called MARS, which provides a rich debugging GUI. You can run MARS on your home computer by downloading the jar file from the Internet or by copying it from ~cs61c/bin/Mars4_5.jar on the instructional machines. You will need Java J2SE 1.5.0 (or later) SDK installed on your computer, which can be obtained from Sun. If your home computer is a Mac, you can also follow the instructions here to install MARS as an app in one step (thanks to Sagar, a former TA).
You can run MARS in lab by typing 'mars &' at the command line.The ampersand is optional but will allow you to continue using that terminal window (on the Macs however, you'll need to run it in it's own terminal tab by pressing command-t first).To run the program remotely, you may either run via an instructional server (but NOT one of the Orchard machines), or through a local installation (recommended).When on an instructional server, you will need to be running an X-Server (like XMing), and enabling X11 tunneling.
Tip: Although it is possible, you should avoid running MARS remotely at all costs - it will be painfully slow to use and will overwhelm the servers if many students attempt to do so.It is in your best interest to setup/run a local copy of MARS. I know several students will try anyways, but consider this your warning.
Assembly Basics:
- Assembly programs go in text files with a .s extention.
- Programs must contain a label 'main:' (similar to the main() function in C programs).
- Programs must end with an 'addi $v0,$0,10' followed by a 'syscall'. main() is special and must transfer control back to the operating system when it is done rather than just returning.
- Labels end with a colon (:).
- Comments start with a pound sign (#).
- You CANNOT put more than one instruction per line.
Exercises
Exercise 1: Familiarizing yourself with MARS
Getting started:
- Run MARS.
- Load lab3_ex1.s using File-->Open.
- View and edit your code in the 'Edit' tab. Notice the code highlighting and 'completion suggestion' features.
- When ready, assemble your code using Run-->Assemble (or press F3).
- This will take you automatically to the 'Execute' tab, which is where you can run and debug your program.
- Step through the program using Run-->Step (or press F7).
- You should take the time to familiarize yourself with everything in the Run menu (and the keyboard shortcuts).
For this exercise, we calculate the Fibonacci numbers using fib[0] = 0; fib[1] = 1; fib[n] = fib[n-1] + fib[n-2].
Follow the steps below and record your answers to the questions. The Help menu (F1) may come in handy.
- What do the .data, .word, .text directives mean (i.e. what do you use them for)?
- How do you set a breakpoint in MARS? Set a breakpoint on line 14 and run to it. What is the instruction address? Has line 14 executed yet?
- Once at a breakpoint, how do you continue to execute your code? How do you step through your code? Run the code to completion.
- Find the 'Run I/O' window. What number did the program output? If 0 is the 0th fib number, which fib number is this?
- At what address is n stored in memory? Try finding this by (1) looking at the Data Segment and (2) looking at the machine code (Code column in the Text Segment).
- Without using the 'Edit' tab, have the program calculate the 13th fib number (0-indexed) by manually modifying this memory location before execution. You may find it helpful to uncheck the 'Hexadecimal Values' box at the bottom of the Data Segment.
- How do you view and modify the contents of a register? Reset the simulation (Run-->Reset or F12) and now calculate the 13th fib number by (1) breaking at a well-chosen spot, (2) modifying a single register, and then (3) unsetting the breakpoint.
- Lines 19 and 21 use the syscall instruction. What is it and how do you use it? (Hint: look in Help)
Checkoff
- Show your TA that you are able to run through the above steps and provide answers to the questions.
Exercise 2: Compiling from C to MIPS
For this exercise we need to use a program called mips-gcc (a cross-compiler for MIPS) that allows us to compile programs for the MIPS architecture on our x86 machines. If you are doing this lab remotely, you should SSH into one of the hive machines.
Compile The file lab3_ex2.c into MIPS code using the command:
The -O2 option (capital 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 lab3_ex2.c are originally stored in the assembly file.Finally, explain how these pointers are manipulated through the loop.
Checkoff
- Find the section of code in lab3_ex2.s that corresponds to the copying loop and explain how each line is used in manipulating the pointer.
Exercise 3
This exercise uses the file listmanips.s.
We might have left Python behind with CS61A, but we definitely want to bring our friends map and reduce along with us! In this exercise, you will complete an implementation of map in MIPS. Our function will be simplified to mutate the list in-place, rather than creating and returning a new list with the modified values.Our map procedure will take two parameters; the first parameter will be the address of the head node of a singly-linked list whose values are 32-bit integers. So, in C, the structure would be defined as:
Our second parameter will be the address of a function that takes one int as an argument and returns an int. We'll use jalr
(see below) to call this function on the list node values.
Our map
function will recursively go down the list, applying the function to each value of the list nodes, storing the value returned in that node. In C, this would be something like this:
If you haven't seen the int (*f)(int)
kind of declaration before, don't worry too much about it. Basically it means that f
is a pointer to a function, which in C is used exactly like any other function.
Mars Mips Macros
You'll need to use an instruction you might not have learned before to implement this: jalr
. jalr
is to jr
as jal
is to j
. It jumps to the address in the given register and stores the address of the next instruction (i.e., PC+4
) in $ra
. So, if I didn't want to use jal
, I could use jalr
to call a function like this:
There are 7 places (6 in map
and 1 in main
) in the provided code where it says 'YOUR_INSTRUCTION_HERE'. Replace these with instructions that perform as indicated in the comments to finish the implementation of map
, and to provide a sample call to map
with square
as the function argument. When you've filled in these instructions, running the code should provide you with the following output:
Checkoff
- Show your TA your test run.
Exercise 4
Add the prologue and epilogue to the code in nchoosek.sso that it computes 'n choose k'. the number of combinations of n distinct elementstaken k at a time. This can be computed through factorials, however we will be computing this through finding the (n,k) entry in Pascal's triangle.
Checkoff
Mars Mips Array
- Show your TA your code and its test run.