True Stack Detectives
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.
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
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.
d153cffbbab79a0c601b99cbe5aa6afbWhat 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.
a?
*a?
a[1]?
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.
ra?
a2?
What is the hexedecimal address of the call arraySum instruction that made the initial call to arraySum?