ISA & Assembly
Which real instruction executes the RISC-V pseudo-instruction "ret"?
- Ajal ra, 0
- Bjalr x0, 0(ra)
- Clw ra, 0(sp)
Show answer and explanation
Answer: B. jalr x0, 0(ra)
jalr (jump and link register) jumps to a register value plus an offset and writes the next instruction's address into the destination. ret is "jalr x0, 0(ra)": it jumps to the address in ra and discards the link in x0. jalr is also used for jumps whose target is decided at run time, such as calls through function pointers or virtual functions. Restoring ra with "lw ra, ..." is preparation done before ret.
- A. It overwrites ra, so it cannot return.
- B. Correct. It jumps to ra and discards the link.
- C. It only loads ra from memory and does not return.