call
pseudolyszt.net covers call and other RISC-V instructions in more detail.
Description
Calls a function at a label. In call label, the return address goes into ra and execution continues at the label; the caller resumes there when the callee returns. The assembler expands it to jal ra, label.
Expands to
jal ra, label
The assembler replaces this pseudo-instruction with the real instruction or instructions listed above.
Official definition
The jump and link (JAL) instruction uses the J-type format, where the J-immediate encodes a signed offset in multiples of 2 bytes. The offset is sign-extended and added to the address of the jump instruction to form the jump target address. Jumps can therefore target a ±1 MiB range. JAL stores the address of the instruction following the jump ('pc'+4) into register rd. The standard software calling convention uses 'x1' as the return address register and 'x5' as an alternate link register.
Plain unconditional jumps (assembler pseudoinstruction J) are encoded as a JAL with rd=x0.
The JAL and JALR instructions will generate an instruction-address-misaligned exception if the target address is not aligned to a four-byte boundary.
Return-address prediction stacks are a common feature of high-performance instruction-fetch units, but require accurate detection of instructions used for procedure calls and returns to be effective. For RISC-V, hints as to the instructions' usage are encoded implicitly via the register numbers used. A JAL instruction should push the return address onto a return-address stack (RAS) only when rd is 'x1' or x5. JALR instructions should push/pop a RAS as shown in rashints.
References
- [1]RISC-V International. The RISC-V Instruction Set Manual, Volume I: Unprivileged Architecture. Version 20260120. Section 2.5.1, Unconditional Jumps, page 30.
- [2]RISC-V International. The RISC-V Instruction Set Manual, Volume I: Unprivileged Architecture. Version 20260120. Section 2.4.1, Integer Register-Immediate Instructions, pages 28 to 29.