ISA & Assembly
What does the RISC-V instruction "jal ra, func" do?
- ASaves the address of the next instruction in ra, then jumps to func
- BOnly puts the address of func in ra and does not jump
- CReturns to the address held in ra
Show answer and explanation
Answer: A. Saves the address of the next instruction in ra, then jumps to func
jal (jump and link) writes the address of the next instruction (PC + 4) into the given register (the link) and jumps to the target. The calling convention puts the return address in ra (x1), so a call is written "jal ra, func", or "call func" as a pseudo-instruction, which expands to an auipc and jalr pair and can also reach distant functions. The called function returns to the address in ra when it finishes. With x0 as the destination, jal becomes a plain jump.
- A. Correct. This is the call instruction.
- B. jal also jumps.
- C. Returning is done by ret (jalr).