C8 · Problem set 5
Problem set 5: functions, the stack and fixed point
These problems use lessons 14 to 23 with the weight on the newest: stack frames and recursion, compiling C, pointers, multiplication, division and fixed point, and the functions lab. The stack starts at sp = 0x10000 (65,536), frames are multiples of 16 bytes, and instructions run one per cycle unless a problem says otherwise.
Check
A function that calls other functions uses 2 saved registers and 8 bytes of local variables.
(a)
How many bytes does it need to store?
(b)
How big is its frame?
Check
A recursive function uses 16-byte frames on an 8192-byte stack.
(a)
How much stack does recursion 50 levels deep use?
(b)
How deep can it go before overflowing?
Check
A leaf function with a 3-instruction body is called 1000 times; each call adds 6 instructions (argument moves, jal, ret). The core runs at 48 MHz.
(a)
How many instructions do the calls run?
(b)
How long does that take?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Check
A function is entered with sp = 65536, makes a 16-byte frame, and saves ra at the top of it.
(a)
What is sp inside the function?
(b)
At what address is ra saved?
Check
A Q-format number has 12 fraction bits.
(a)
What integer stores 0.3?
(b)
What is its resolution, the value of one unit?
Check
0.55 and 0.6 are multiplied in Q15 with rounding: the product of the integers, plus , shifted right by 15.
(a)
What integer results?
(b)
What value does it represent?
Check
A compiler divides an unsigned 16-bit x = 9999 by 10 as (x × 52429) >> 19.
(a)
What is the product?
(b)
What does the shift leave?
Check
A 12-bit ADC with a 2500 mV reference reads 3001.
(a)
What does (c × V) >> N give, in mV?
Write units the way you normally would: 1.5 mV, 1500 µV and 0.0015 V are the same answer.
(b)
How much does dropping the fraction lose?
Check
100 Q15 values, each between −1 and 1, are summed in an integer accumulator.
(a)
How many guard bits above the 16 must it have so the sum can't overflow?
(b)
How wide must the accumulator be?
Check
Two loops count the 1 bits of x = 4097.
(a)
How many passes does the x & (x − 1) loop make?
(b)
How many does a loop make that tests the low bit and shifts right until x is 0?
Check
x = p->next->...->val follows 3 next pointers, then reads val; each load depends on the one before and takes 3 cycles.
(a)
How many loads does it make?
(b)
How many cycles do the loads take?
Check
−101 is divided by 2 to the power 1.
(a)
What does div give (rounding toward zero)?
(b)
What does srai by 1 give?
Check
Q31 numbers for 0.3 and 0.25 are multiplied with mulh, which keeps the high 32 bits of the 64-bit product.
(a)
Read as a Q30 number, what value does mulh return?
(b)
By how many bits must it be shifted left to be Q31 again?
Check
The naive recursive Fibonacci computes F(15).
(a)
How many calls does it make?
(b)
How many frames are live at the deepest point, with F(1) and F(0) returning without one?
Check
Main sums the squares of 1 to 20 by calling square each time. Each pass of its loop runs 6 instructions plus the 4 in square, after 4 to set up and 2 to finish. The core runs at 16 MHz.
(a)
How many instructions run, counting the final failed test?
(b)
How long does that take?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Have paper and a calculator ready. Work each problem out by hand, then enter the answer. Your first try on each one counts toward your score.
Practice
Keep practicing
15 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.