ISA & Assembly

Which real instruction executes the RISC-V pseudo-instruction "ret"?

  1. Ajal ra, 0
  2. Bjalr x0, 0(ra)
  3. 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.
Practice this topic Back to topic

More ISA & Assembly questions

  1. What is the main purpose of defining a calling convention?
  2. In the standard RISC-V calling convention, which registers pass integer arguments and return values?
  3. In the standard RISC-V calling convention, when calling a function with ten integer arguments, where are the ninth and tenth passed?
  4. In the RISC-V calling convention, s0-s11 are callee-saved registers. What does that mean?
  5. In the RISC-V calling convention, t0-t6 are caller-saved. If a function wants to keep using a value in t0 after calling another function, what should it do?