ISA & Assembly
In RISC-V, what happens if a recursive function (one that calls itself) forgets to save ra on the stack?
- AThe inner call overwrites ra, so the outer invocation cannot return to its real caller
- BNothing goes wrong, because the CPU saves ra automatically
- CThe result is doubled regardless of recursion depth
Show answer and explanation
Answer: A. The inner call overwrites ra, so the outer invocation cannot return to its real caller
The recursive jal overwrites ra with "the instruction after my own call". If ra was not saved, when the outer invocation executes ret it returns into itself instead of to its original caller, looping or misbehaving. That is why any function that calls another must save ra on the stack in its prologue and restore it in its epilogue.
- A. Correct. It keeps returning to just after its own jal.
- B. RISC-V jal only overwrites ra.
- C. The result is not scaled by a fixed factor.