The Truer Than True Stack Detectives

The questions below are due on Sunday November 15, 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

This is another stack study question taken from a previous exam. Like in Truest Stack Detectives, we suggest trying to answer everything in one attempt like a real exam.

The simplest way to compute a**n, for non-negative integers a and n, would be multiplying a x a x a ... n times, resulting in n-1 total multiplications.

A faster way is to do it recursively as follows:

  1. If n is even, first find a**(n/2) and then square it. a**n = (a**(n/2))**2
  2. If n is odd, first find a**((n-1)/2), square it and multiply it by a. a**n = a * (a**((n-1)/2))**2

In this way, we group several smaller powers into one value and square it, saving a lot of multiplications.

Here is a C implementation of the function fastPow that takes in two integers a and n and finds a**n using the methodology described above.

int fastPow(int a, int n) {
    if (n==0) {
        return 1;
    }
    int res = fastPow(a, n / 2);
    res = res * res;
    if (n % 2 == 1) {
        res = a * res;
    }
    return res;
}

In RISC-V, this function has the equivalent implementation shown below. Note that we also need a mult procedure to perform the multiplication between two numbers as we do not have an instruction for it in RV32I from class. We have not provided the implementation for simplicity but you can assume that mult does not store anything on the stack, nor does it modify the stack pointer. Here is a C declaration for this function.

int mult(int a, int b); // returns a * b
fastPow:
    bne a1, x0, fp_body
    addi a0, x0, 1
    jalr x0, 0(ra)
fp_body:
    addi sp, sp, -12
    sw ra, 0(sp)
    sw a0, 4(sp)
    sw s0, 8(sp)
    ___BLANK__
    srli a1, a1, 1
    jal ra, fastPow
    addi a1, a0, 0
    jal ra, mult         # compute res*res
    beq s0, x0, fp_done
    lw a1, 4(sp)
    jal ra, mult         # compute a*res
fp_done:
    lw ra, 0(sp)
    lw s0, 8(sp)
    addi sp, sp, 12
    jalr x0, 0(ra)

Complete the black in the assembly code with the correct RISC-V instruction. Hint: this instruction will be related to line 7 in the equivalent C code.

fastPow is run to find the 26th power of an unknown number (i.e. n = 26 for the initial call to fastPow.) Execution was halted just before a call to mult. We get the following snapshot of register a0 and a1 as well as a relevant portion of the stack. We also know the sp is between 0x31480 and 0x314ff.

Again, as a reminder, the mult function does not modify the stack or the stack pointer at all.

a0 = 0x00000040 a1 = 0x00000040
Address Data
0x00031460 ???
0x00031464 ???
0x00031468 ???
0x0003146c ???
0x00031470 ???
0x00031474 ???
0x00031478 0x00003420
0x0003147c 0x00000002
0x00031480 0x00000000
0x00031484 0x00003420
0x00031488 0x00000002
0x0003148c 0x00000001
0x00031490 0x00003420
0x00031494 0x00000002
0x00031498 0x00000000
0x0003149c 0x0000723c
0x000314a0 0x00000002
0x000314a4 0x00000000
0x000314a8 0x12010fff
0x000314ac 0x12951a7f

What is the address of the label fp_done? 0x

What is the value of the argument a (base of the exponent) to the initial call to fastPow a = 0x

Knowing that n = 26 for the initial call to fastPow, what is the value stored in register sp at the moment the execution was halted? If you cannot determine this value, write CAN'T TELL. sp = 0x

Fill out the values at each memory address below in 32-bit hexadecimal. If you cannot determine the value, write CAN'T TELL.

0x31460

0x31464

0x31468

0x3146c

0x31470

0x31474

Back to Exercises