ISA & Assembly

What is an advantage of using PC-relative addressing for branches and jumps?

  1. AThe branch can always reach any target with one instruction, however far
  2. BBranch prediction becomes unnecessary and the pipeline never stalls
  3. CThe program runs without modifying its instructions no matter where in memory it is loaded
Show answer and explanation

Answer: C. The program runs without modifying its instructions no matter where in memory it is loaded

A PC-relative branch states its target as "so many bytes from here", so if the whole program is shifted to another address the distance is unchanged and it still works. This is the foundation of position-independent code (PIC) used in shared libraries. The distance field has limited bits, though, so far jumps combine auipc and jalr. The need for branch prediction is unrelated to how the target is encoded.

  • A. The immediate field is limited, so the reach is limited.
  • B. How the target is encoded has nothing to do with branch prediction.
  • C. Correct. It makes position-independent code easy.
Practice this topic Back to topic

More ISA & Assembly questions

  1. Which statement about instruction length in the RISC-V base ISA and in x86 is correct?
  2. What is the main advantage of making instructions a fixed length?
  3. In RISC-V instructions, each register field (rd, rs1, rs2) is 5 bits wide. Why?
  4. The immediate of RISC-V addi is a 12-bit signed integer. What range can it represent?
  5. In RISC-V, what is the usual way to put a 32-bit constant that does not fit in a 12-bit immediate (e.g. 0x12345678) into a register?