Name: Date:


  1. In the following assembly code fragment, registers x5 and x6 are initialized to N and zero respectively.

    LOOP: beq  x5, x0, DONE
          addi x6, x6, 1
          addi x5, x5, -1
          jal x0, LOOP

    a. (2 points) What is the value in register x6 for when the code reaches the label DONE:?

    b. (2 points) How many RISC-V instructions are executed?

    c. (6 points) If the registers x5 and x6 correspond to variables i and a respectively, write the equivalent C code for the loop.

  1. Consider the following C code fragment.

    for (i=0; i<a; i++) {
      a = a + b;

    a. (4 points) Assuming that the values of i, a, and b in registers x5, x6 and x7, translate the C code to RISC-V assembly. Use a minimum number of instructions.

    b. (4 points) If the variables a and b are initialized to 10 and 1, what is the total number of RISC-V instructions executed?