CS61C Fall 2017 Lab 4 - RISC-V Functions and Pointers

Hello, gorgeous. Whoa there, when's the last time you had to tell your lab instructions that they were being too forward?

Welcome to Lab 4. It's been a while since you've been here, huh? Since that's the case, why don't we have a look at not one but TWO images of the day! I really hope these make you smile :)

Bonus: Describe, in one sentence, what the similarity between these two images is.


Setup, literally copied and pasted from last time

Copy the lab files from the instructional servers to your lab account with

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

Alternatively, secure-copy (scp) them from the instructional servers to your own laptop with

$ scp -r cs61c-xxx@hive10.cs.berkeley.edu:~cs61c/labs/04/ ~/YOUR_FILEPATH

And if you want to secure-copy them back to your class account:

$ scp -r ~/YOUR_FILEPATH/04 cs61c-xxx@hive10.cs.berkeley.edu:~/YOUR_LABACCT_FILEPATH

Exercise 1: Debugging megalistmanips.s

A long time ago, your TA Julian was a RISC-V rookie, and he wrote his solution to a lab exercise inside megalistmanips.s. You, a RISC-V expert, are tasked with fixing Julian's bugs.

Task: Find the mistakes inside the map function in megalistmanips.s. Before you do that, familiarize yourself with what this function is trying to do. It's a little different from last time.

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);

The task: 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]

Thanks for doing that exercise! I'm sure Julian is wondering where you were to help him when he was writing his misguided RISC-V way back when...

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:

I'm not going to lie: this is a pretty dumb function. Nonetheless, your task is to implement it in RISC-V, with the condition that your code may NOT use any branch instructions!

Luckily, somebody has randomly left an array of integers in the .data section of discrete_fn.s. How can you use this to your advantage and complete this seemingly impossible task?

Checkoff [2/2]