Stack Detective

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

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

What is the missing C code corresponding to ??? 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.

Placeholder for Diagram 8b52787656cce6371ac0521e7c293f09

The Stack

Based on this stack, what was the hexadecimal argument of the most recent call to fn? 0x

What is the missing value marked ??? in the stack above?

What is the hex address of the instruction tagged rtn:? 0x

What was the hexadecimal argument to the first recursive call to fn? 0x

What is the hex address of the jal instruction that called fn originally? 0x

What were the hex contents of s1 at the time of the original call? 0x

What hex value will be returned to the original caller if the value of 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

Examining the assembly language for get_remainder, what is the appropriate C code for ??? 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:

Placeholder for Diagram 0a849bdaff142d27bcbfce46a42112bc

The Stack

For all of the following questions, suppose that during the initial (non-recursive) call to get_remainder, sp pointed to the memory location containing 0x10.

What is the hexadecimal value of the ??? 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.

Value in ra: 0x

Value in a0: 0x

Value in a1: 0x

Value in sp: 0x

What is the hex address of the initial call instruction to get_remainder? 0x

What value will be returned to the original caller? 0x

Back to Exercises