Stack Detective
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.
Exercise 1
You are given an incomplete listing of a C program and its translation to RISC-V assembly code (both shown below):int fn(int x) {
int lowbit = x & 1;
int rest = x >> 1;
if (x == 0) return 0;
else return ???;
}
fn:
addi sp, sp, -12
sw s0, 0(sp)
sw s1, 4(sp)
sw ra, 8(sp)
andi s0, a0, 1
srai s1, a0, 1
yy:
beqz a0, rtn
mv a0, s1
jal ra, fn
add a0, a0, s0
rtn:
lw s0, 0(sp)
lw s1, 4(sp)
lw ra, 8(sp)
addi sp, sp, 12
jr ra
??? in the above program? Select the best answer from the choices below:
The procedure fn is called from an external procedure and its execution is interrupted just prior to the execution of the instruction tagged yy:. The contents of a region of memory during one of the recursive calls to fn are shown below.
8b52787656cce6371ac0521e7c293f09
fn? 0x
??? in the stack above?
rtn:? 0x
fn? 0x
jal instruction that called fn originally? 0x
a0 at the time of the original call was 0x47? 0x
Exercise 2
The following C program implements the function get_remainder(x, y) of two arguments, which returns the remainder when x is divided by y. The assembly code for the procedure is also shown below:
int get_remainder(int x, int y) {
int difference = x - y;
if (difference < 0) return x;
else return ???;
}
get_remainder:
sub t0, a0, a1
bltz t0, rtn
addi sp, sp, -4
sw ra, 0(sp)
mv a0, t0
jal get_remainder
lw ra, 0(sp)
addi sp, sp, 4
rtn:
jr ra
??? in the C representation of get_remainder?
The execution of the procedure call get_remainder(0x68, 0x20) has been suspended just as the processor is about to execute the instruction labeled rtn: during one of the recursive calls to get_remainder. A portion of the stack at the time execution was suspended is shown below:
0a849bdaff142d27bcbfce46a42112bcFor all of the following questions, suppose that during the initial (non-recursive)
call to get_remainder, sp pointed to the memory location containing 0x10.
??? in the stack dump that the stack pointer is pointing to? 0x
Determine the specified values at the time execution was suspended. If the value cannot be determined, write “CAN'T TELL”. Otherwise, express the value in hex.
ra: 0x
a0: 0x
a1: 0x
sp: 0x
get_remainder? 0x