Try   HackMD

CA-2019Fall: Quiz 3

A

You are given the following C code along with an incomplete translation of this code into RISC-V assembly. Assume that execution ends when it reaches the unimp instruction. (unimp means "unimplmented")

Complete the RISC-V translation by:

  1. Inserting the loop label where it belongs in the code.
  2. Filling in the blank box with the missing instruction.

B: continuing A

Provide the values left in the registers below following the execution of the
corrected code:
Value left in t0: 0x____________
Value left in t1: 0x____________
Value left in a2: 0x____________


C

For each RISC-V instruction sequence below, provide the hex values of the specified registers after each sequence has been executed. Assume that all registers are initialized to 0 prior to each instruction sequence. Each instruction sequence begins with the line (. = 0x0) which indicates that the first instruction of each sequence is at address 0. Assume that each sequence execution ends when it reaches the unimp instruction.


D

Assume that these memory locations have been initialized with the following data. The data consists of 3 words beginning at address 0x100, another 3 beginning at address 0x200, and finally a third set beginning at address 0x1000.



E

Below is the C code for a recursive version of the factorial function. And then following a translation to RISC-V assembly language.

  • C code
int mul(int a, int b) {
    return a * b;
}
int factorial(int n) {
    if (n <= 1) return 1;
    int f1 = factorial(n - 1);
    return mul(f1, n);
}
  • RISC-C Assembly

(1) What should be in the blank on the line labeled L1 to make the function operate
correctly?
L1: lw ____________

(2) How many words will be written to the stack before the program makes each recursive call to the factorial function?
Number of words pushed onto stack before each recursive call? __________

The factorial code above and a program that calls it once (using call factorial and with an argument greater than 1) are loaded into the RISC-V simulator. A breakpoint is set on the line labeled L2 and the program is executed until it reaches the breakpoint. The diagram below shows a small piece of the stack at this point. All addresses and data values are shown in hex. The current value in the SP register is 0xF7D8 and points to the location shown in the diagram.

(3) Fill in each of the blanks to the right of the diagram to indicate which register the value came from when it was pushed onto the stack. Write an X if it is not possible to determine from the information you have


Solutions

A

loop:
	slli t0,t0,2

B

  • t0: 0x808
  • t1: 0x4
  • t2: 0x2

C

  • x2: 0x2000
  • x3: 0x2009
  • x4: 0xFFFFDFF6

D

  • x6: 0x12345678
  • x7: 0x100
  • x8: 0x77773333

E

  • (1) ra, 0(sp)
  • (2) 2
  • (3)
    • 0x2: X(impossible)
    • 0x80: ra
    • 0x2: a0