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

More coming soon!

2Conditionally Branching Far Away

Conditional Branches are typically used for if-else statements, for/while loops. In general, because these control structure are usually pretty small (<50 lines of C code), we can use B-Type instructions. However, as we saw earlier, B-Type instructions have limited range: ±210\pm 2^{10} instructions from the current instruction (PC).

To jump even further, we can use J-Type unconditional jumps in combination with B-Type instructions.

Admittedly, J-Type instructions also have a limited range: ±218\pm 2^{18} instructions from the currrent insturction (PC).

If we wanted to jump to any address, RISC-V opts for jumping with absolute addressing and jalr. As discussed in a previous section, jalr is an I-Type instruction that sets PC to PC = R[rs1] + imm, where imm specifies a 12-bit immediate imm.

See the discussion of U-Type instructions lui and auipc. Just like with resolving li pseudoinstructions, jalr sign-extends immediates. So if lo12bits has sign bit set, increment hi20bits by 1.

Footnotes
  1. The jalr instruction will save the jump address to the PC and save the current PC + 4 to the ra in the same cycle (effectively, “simultaneously”)