C8 Computer organization · Practice
Recursion and stack frames
Problems dealt one at a time, with new numbers every time. The kinds you miss come back more often, and you can keep going as long as you like.
0 solved
0 in a row
0 of 8 kinds solid
Check
A recursive function with a 16-byte frame is called with n = 100; levels n down to 2 each make a frame before recursing, and level 1 returns at once. How much stack is in use at the deepest point?
Check
How many calls does the naive recursive Fibonacci make to compute F(8)?
Check
With n in a0, leave the Fibonacci number F(n) in a0, where F(0) = 0, F(1) = 1 and each later one is the sum of the two before. (A recursive version or a loop: either is fine.)
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
A naive recursive Fibonacci computes F(10) with 16-byte frames.
(a)
How much stack does it use at the deepest point?
(b)
How many additions does a loop need instead?
Check
With n in a0 (at most 12), put n! into a1.
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
How much stack do 20 live frames of 16 bytes take?
Check
A function needs 40 bytes of locals and saved registers. How large is its frame, keeping sp a multiple of 16?
Check
A function recursing 6 levels deep with 16-byte frames is rewritten as a loop that uses a single frame. How many bytes of stack does that save at the deepest point?
A kind of problem counts as solid once you've got it right on the first try twice in a row. Back to the lesson · Practice every topic in C8