jpseudo

j

pseudo
RISC-V Assembly Reference

lyszt.net covers j and other RISC-V instructions in more detail.

Description

Branches to a label without storing a return address. In j label, execution continues at the label. The assembler expands it to jal with the destination zero, and no return address is written.

Expands to

jal zero, label

The assembler replaces this pseudo-instruction with the real instruction or instructions listed above.

Official definition

RISC-V Instruction Set Manual

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.