A function calls a helper in a loop and never comes back. Give it a stack frame, and save exactly what the calling convention says a call may destroy.
Hardware context: Two hand-compiled functions on a small model of an RV32I processor: a caller that passes three arguments and a callee that returns their sum. Reading compiled code in a trace and writing assembly that can be called from C
The function total(p, n) adds up scale(p[i]) for the n words of an array. The helper scale belongs to another team. Your function was tested with a stand-in for scale that changed no register, and it passed.
With the real scale, the function never returns. Repair it. You write the function as rows, one for each instruction, as in the lab of the lesson.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
trial | 10 | input of each check |
Full specification
| Function | Where | Arguments | Result |
|---|---|---|---|
| total, yours | address 0 | a0: the address of the array. a1: the number of words, 1 or more | a0: the sum of scale of each word |
| scale, of the bench | address 'h100 | a0: one word | a0: a value worked out from it |
| Registers | After a call of scale |
|---|---|
| a0 | the result |
| a1 to a7, t0 to t6 | changed |
| ra | the return address of that call |
| s0 to s11, sp | as they were |
| memory below your sp | the 16 bytes just below it are overwritten |
How to read a result: returned_to_caller is 1 when your function came back to the bench. saved_registers_kept and sp_restored are 1 when s0 to s11 and sp hold what they held at the call. memory_outside_frame_unchanged is 1 when nothing was written at or above the starting sp, or more than 512 bytes below it. calls_of_scale counts the calls, and a0 is the result, in decimal.
Examples
Example 1
Three Words- Input
an array of 3 words at 8000_0100; sp starts at 8000_fff0
- Output
returned_to_caller = 1, saved_registers_kept = 1, sp_restored = 1, memory_outside_frame_unchanged = 1, calls_of_scale = 3, a0 = 6300
The function calls scale three times and returns to the bench. Every saved register and sp are as they were.
Example 2
One Word- Input
an array of 1 word at 8000_0200; sp starts at 8000_f000
- Output
returned_to_caller = 1, saved_registers_kept = 1, sp_restored = 1, memory_outside_frame_unchanged = 1, calls_of_scale = 1, a0 = 4306
One call. Even one call destroys ra and the temporary registers.
Example 3
Six Words- Input
an array of 6 words at 8000_0040; sp starts at 8000_ff00
- Output
returned_to_caller = 1, saved_registers_kept = 1, sp_restored = 1, memory_outside_frame_unchanged = 1, calls_of_scale = 6, a0 = 46911
Six calls. The frame is taken once and given back once.
Constraints and Contract
- Call scale once for each word, with the word in a0. Do not work its result out yourself.
- Return with the PC equal to the value that ra held when your function was called.
- Hand back s0 to s11 and sp with the values they had at the call. The bench holds values of its own in them.
- Use only memory from the starting sp downward, at most 512 bytes. Keep sp a multiple of 16.
- The function has at most 64 rows.
Hints
Hint 1
List every register that holds a value across the call of scale. Then look each one up in the table.
Hint 2
What does the jal do to ra, and what did ra hold before?
Hint 3
A saved register is safe from scale. But your caller expects its old value back from you.
What the grader checks: Arrays of 1 to 255 words. The stack starts at 13 different addresses. Ten random calls. Every register that the function is not given holds a leftover value. The bench checks the result, the number of calls of scale and where the function returns. It also checks every saved register, sp, and all memory outside the 512 bytes below the starting sp.
23 checks on Icarus Verilog · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your source only; samples require the simulation service. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 2 problems on this path