CS61C Fall 2018 Lab 5 - RISC-V Functions and Pointers

Goals

Getting the files

Like with previous weeks, cd to your repository and then get the lab 3 starter files via:

        $ git fetch starter
        $ git merge starter/master

Exercise 1: Debugging megalistmanips.s

In Lab 3, you completed a RISC-V procedure that applied a function to every element of a linked list. In this lab, you will be working with a similar (but slightly more complex) version of that procedure.

Now, instead of having a linked list of ints, our data structure is a linked list of int arrays. Remember that when dealing with arrays in structs, we need to explicitly store the size of the array. In C code, here's what the data structure looks like:

struct node {
    int *arr;
    int size;
    struct node *next;
};

Also, here's what the new map function does: it traverses the linked list and for each element in each array of each node, it applies the passed-in function to it, and stores it back into the array.

void map(struct node *head, int (*f)(int)) {
    if (!head) { return; }
    for (int i = 0; i < head->size; i++) {
      head->arr[i] = f(head->arr[i]);
    }
    map(head->next, f);
}

Task: Find the mistakes inside the map function in megalistmanips.s (Venus Magic Link). Read all of the commented lines under the map function in megalistmanips.s (before it returns with jr ra), and make sure that the lines do what the comments say.

Some hints:

Checkoff [1/2]

Exercise 2: Write a function without branches

Consider the discrete-valued function f defined on integers in the set {-3, -2, -1, 0, 1, 2, 3}. Here's the function definition:

Your task is to implement this function in RISC-V, with the condition that your code may NOT use any branch and/or jump instructions!

Luckily, you have access to an array of integers in the .data section of discrete_fn.s (Venus Magic Link). How can you use this to your advantage?

Checkoff [2/2]