CS61C Summer 2016 Lab 3 - MIPS Assembly

Goals

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

$ cp -r ~cs61c/labs/03/ ~/labs/03/ 

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:

Exercises

Exercise 1: Familiarizing yourself with MARS

Getting started:

  1. Run MARS.
  2. Load lab3_ex1.s using File-->Open.
  3. View and edit your code in the "Edit" tab. Notice the code highlighting and 'completion suggestion' features.
  4. When ready, assemble your code using Run-->Assemble (or press F3).
  5. This will take you automatically to the "Execute" tab, which is where you can run and debug your program.
  6. Step through the program using Run-->Step (or press F7).
  7. 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.

  1. What do the .data, .word, .text directives mean (i.e. what do you use them for)?
  2. 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?
  3. Once at a breakpoint, how do you continue to execute your code? How do you step through your code? Run the code to completion.
  4. Find the "Run I/O" window. What number did the program output? If 0 is the 0th fib number, which fib number is this?
  5. 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).
  6. 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.
  7. 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.
  8. Lines 19 and 21 use the syscall instruction. What is it and how do you use it? (Hint: look in Help)

Checkoff

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:

$ mips-gcc -S -O2 -fno-delayed-branch -I/usr/include lab3_ex2.c -o lab3_ex2.s

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

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:

struct node {
  int value;
  struct node *next;
};

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:

void map(struct node *head, int (*f)(int))
{
  if(!head) { return; }
  head->value = f(head->value);
  map(head->next,f);
}

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.

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:

# I want to call the function garply, but not use jal.
la $t0 garply # so I use la to load the address of garply into a register ($t0)
jalr $t0      # and then use jalr to jump and link to it.

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 mapwith square as the function argument. When you've filled in these instructions, running the code should provide you with the following output:

List Before: 9 8 7 6 5 4 3 2 1 0
List After: 81 64 49 36 25 16 9 4 1 0

Checkoff

Exercise 4

Add the prologue and epilogue to the code in nchoosek.s so that it computes "n choose k". the number of combinations of n distinct elements taken 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