Relative addressing

From C64-Wiki
Jump to navigationJump to search

Relative addressing is the addressing mode used by all conditional-branch instructions in the 65xx instruction set: Beyond the opcode for the instruction itself, all these instructions take up a single, signed-integer byte that specifies, in relative terms, how far "up" or "down" to jump if the required conditions are met: This signed 8-bit figure is called the offset.

If a branching instruction is situated at address a, and needs to branch to a given address b, the single-byte offset argument that should follow the opcode is calculated as ba − 2.
Conversely, if a branching instruction at address a has an offset argument of value F, the destination address for the branch (if taken), is a + F + 2

The signed offset needs to fit in a single byte, so this scheme imposes a limit to how far a branching instruction can actually "branch", or jump; no further than 126 bytes "backwards", or 129 bytes "forwards" relative to the location of the branching instruction.

There are eight branching instructions, all of which support only this relative addressing mode: They are BCC, BCS, BEQ, BMI, BNE, BPL, BVC, and BVS.