Skip to content

Exam-like Problems - Base Conversions

Your worked out solutions are due Mon Oct 6th at 11:59pm You must submit problems.pdf in your lab07 GitHub Repo

Question 1 - C to Assembly

Consider the following C function. Provide and English description of what this function does and provide the RISC-V implementation of this function.

int count_rec_c(char *str, char c) {
    int addval = 0;

    if (str[0] == '\0') {
        return 0;
    } else {
        if (str[0] == c) {
            addval = 1;
        }
        return addval + count_rec_c(&str[1], c);
    }
}

Question 2 - Structs and Assembly

Given what we have learned about how structs are layed out in memory, consider the following struct and answer the following questions.

struct node_st {
    char id[2];
    int value;
    struct node_st *next_p;
};
How many actual bytes will this struct use in memory when considered alignment and padding?

In RISC-V Assembly, how do you load the next_p field value into t0?

Question 3 - RISC-V Machine Code

Lets assume that we have a new RISC-V instruction format called the x-type:

 31                 20 19       15 14    12 11       7 6       0
|   imm[5:0|11:6]     |    rs1    | funct3 |    rd    |  opcode |

Assume you have this instruction word in a C uint32_t variable called iw. Write a C code snippet that can c onstruct the a 32 bit signed immediate value from iw, which is a int32_t type called imm. Your code snipp et should just use C variables and expressions, no function calls. You cannot use get_bits() or sign_extend( ).

Question 4 - Cache Memory

Assume you have a 2-way Set Associative Case with 32 total words and a block size of 2 words. How many total se ts does this cache have? How many set index bits are there in a 64 bit address?