ISA & Assembly

In RISC-V, what happens if a recursive function (one that calls itself) forgets to save ra on the stack?

  1. AThe inner call overwrites ra, so the outer invocation cannot return to its real caller
  2. BNothing goes wrong, because the CPU saves ra automatically
  3. 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.
Practice this topic Back to topic

More ISA & Assembly questions

  1. On little-endian RISC-V, a register holding 0x12345678 is stored with sw to address 0x100. What value does lbu then read from address 0x101?
  2. In RISC-V, what can happen when the address of a 4-byte lw is not a multiple of 4 (misaligned)?
  3. The RISC-V instruction "slli t0, a0, 3" makes t0 how many times the value of a0 (ignoring overflow)?
  4. a0 holds the start address of an int (4-byte) array a, and a1 holds an index i. What does the code above load into a2?
  5. Which statement is correct about designs like x86, where an arithmetic instruction such as "add eax, [rbx]" can use a memory value directly?