C8 · Problem set 3
Problem set 3: branches, loops, data and functions
These problems use lessons 9 to 15: branches and loops, arrays and strings in memory, the stack and the calling convention, and recursion. Instructions are 4 bytes, the stack starts at sp = 0x10000 and grows down, and frames are kept a multiple of 16 bytes.
Check
A loop has 3 setup instructions, then a body of 4 instructions (its test included) that runs 1000 times, and one final test that leaves the loop. Each instruction takes one cycle at 50 MHz.
(a)
How many instructions run in all?
(b)
How long does it take?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Check
A branch at 1130 jumps back 3 instructions, to the top of its loop.
(a)
What offset is stored in the branch?
(b)
What is the target address?
Check
jal ra, f sits at 1100, and the function f starts at 1400.
(a)
What does ra hold after the jump?
(b)
What offset does the jal hold?
Check
a0 = −5 and a1 = 1.
(a)
What is a0 read as an unsigned number?
(b)
What does slt a2, a0, a1 put in a2?
(c)
What does sltu a2, a0, a1 put in a2?
Check
An array of 5 words starts at 2000, and a loop walks a pointer through it.
(a)
Where does the end pointer, just past the last element, point?
(b)
Where is the last element?
Check
A C string of 5 characters starts at 1000.
(a)
At what address is its terminating zero?
(b)
How many bytes does it occupy?
Check
The halfword 9C40 is in memory.
(a)
What does lh load?
(b)
What does lhu load?
Check
A function calls other functions and uses 2 saved registers (s0 upward) for values that must survive those calls.
(a)
How many registers must it save on the stack?
(b)
How large is its frame, rounded up to a multiple of 16 bytes?
Check
A recursive function makes one 16-byte frame per level and goes 20 levels deep before the base case, which makes no frame. sp starts at 0x10000.
(a)
How much stack is in use at the deepest point?
(b)
What is sp at the deepest point?
Check
The naive recursive Fibonacci computes F(8).
(a)
How many calls does it make?
(b)
How many additions does a loop starting from F(0) and F(1) need instead?
Check
A count-down loop runs addi t1, t1, -1 then bnez t1, loop each pass.
(a)
If t1 starts at 10, how many passes does it make?
(b)
If t1 starts at 0 by mistake, how many passes does it make?
Check
A function takes 10 word-sized arguments. The calling convention passes the first eight in a0 to a7 and the rest on the stack.
(a)
How many arguments go on the stack?
(b)
How many bytes of stack do they take?
Check
A recursive fact(n) uses 16-byte frames, and fact(1) returns without making one. The stack holds 4096 bytes.
(a)
How much stack does fact(12) use at its deepest?
(b)
What is the largest n whose recursion fits?
Check
A function uses 3 saved registers and 6 bytes of locals; it calls other functions if c = 1 (here c = 0).
(a)
How many registers does it save in its frame?
(b)
How big is its frame?
Check
A loop reverses a string of 25 bytes in place by swapping from both ends; each swap pass runs 8 instructions, after 4 to set up.
(a)
How many swaps does it make?
(b)
How many instructions does it run?
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.