Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1Learning Outcomes

2Jump instructions

Transferring control between procedures simply means unconditional jumps to different program instructions. Notably, if one procedure (caller) calls another (callee), the callee must know how to return to the caller.

Recall that unconditional jumps are instructions that, when executed, set PC to a different instruction. We therefore need jump instructions that keep track of instruction return addresses.

Above, “save the return address” means to save the address of the next instruction, PC + 4, into the named register ra. “Jump to an address” means to update the PC so that on the next cycle, the computer executes a different, out-of-order instruction.

Let’s discuss this in more detail below.

2.1Jump pseudoinstructions vs. real instructions

Unconditional jumps are not particularly tricky to understand (we hope). However, it is important to note that of their many use cases, there are only two real unconditional jump instructions shown in Table 1: jal and jalr. The rest (jr, ret, j, another jal) are pseudoinstructions.

Table 1:Unconditional jumps; see RISC-V green card for Control and Pseudoinstructions.

Instruction or PseudoinstructionNameDescriptionIf pseudo, translation
jal rd labelJump And LinkR[rd] = PC + 4;
PC = PC + offset
-
jalr rd rs1 immJump And Link RegisterR[rd] = PC + 4;
PC = R[rs1] + imm
-
j labelJumpPC = PC + offsetjal x0 label
jal labelJump And Link (Pseudo)R[ra] = PC + 4
PC = PC + offset
jal ra label
jr rs1Jump RegisterPC = R[rs1]jalr x0 rs1 0
retRETurn (jr ra)PC = R[ra]jalr x0 ra 0

There are two real instructions above.

Jump and Link (jal rd label). Write the address of the next instruction, PC + 4, to register rd. Then perform an unconditional jump to label by setting PC to the address of the instruction with label label. The linking means that we form a link that can be used to return to the caller. (In this respect, jal should really be called “Link and Jump”).

Jump and Link Register (jalr rd rs1 imm). Link the “return address” (PC + 4) to a register rd. Then perform an unconditional jump by setting PC to R[rs1] + imm.

3leaf Function Example

4RISC-V Stack Frames

In a previous section we have already seen how we can store and load arrays to and from the stack. In this section we discuss how stack frames get allocated and deallocated between function calls.

When we discussed the C stack, we saw an animation that pushed and popped stack frames between function calls. Importantly:

The stack grows downward. The stack pointer (sp) points to the top of the stack, i.e., the address of the current stack frame.

RISC-V stack frames (mostly) operate like C stack frames. As discussed in an earlier section, the stack pointer holds the address of the top of the stack. By [RV32I register convention], this value is stored in the sp register, which is register number x2.

A RISC-V proedure can choose to use a stack frame by manipulating sp:

The slidedeck in Figure 1 animates allocation and deallocation on the stack via the stack pointer.

Figure 1:An extended animation of stack memory management in RISC-V.

Like in C, pushing and popping stack frames simply corresponds to decrementing and incrementing the the stack pointer. A previous callee’s data may therefore stay in memory that is marked as “free” for the next callee to scribble over it. Refer to the C stack discussion for potential security issues.