True Stack Detectives

The questions below are due on Sunday November 01, 2026; 11:59:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Back to Exercises

The following C program computes the sum of all elements in an array.

int arraySum(int* a, int b){
    // int *a: pointer to array
    // int b: length of array
    if (b == 1) return a[0];
    else {
        return a[0] + arraySum(a+1, b-1);
    }
}

The corresponding assembly program is shown below:

arraySum:
     lw a2, 0(a0)
     li a3, 1
     beq a1, a3, end
     addi sp, sp, -8
     sw ra, 0(sp)
     sw a2, 4(sp)
     addi a0, a0, 4
     #REPLACE <---
     jal arraySum
     lw a2, 4(sp)
     add a2, a2, a0
     lw ra, 0(sp)
     addi sp, sp, 8
end:
mv a0, a2
ret

What RISC-V assembly instruction should go in the line marked REPLACE in order to make the assembly implementation match the C program?

The program’s initial call to arraySum is made from outside of the function. arraySum is interrupted during a recursive call, just prior to the execution of mv a0, a2. The diagram below shows the contents of the stack at this point in time.

Placeholder for Diagram d153cffbbab79a0c601b99cbe5aa6afb

The Stack

What are the (hexadecimal) values of the following variables at the time of the initial call to arraySum? Write CAN'T TELL if you cannot tell a value from the information provided.

Initial value of a?

Initial value of *a?

Initial value of a[1]?

Initial value of b?

What are the (hexadecimal) values in the following registers when the program is halted? Write "CAN'T TELL" if you cannot tell a value from the information provided.

Current value of ra?

Current value of a2?

What is the hexedecimal address of the call arraySum instruction that made the initial call to arraySum?

Address of initial call (specify as a 32 bit hexadecimal value)? 0x

Back to Exercises