Addressing Modes — REL
Relative addressing is used only by branch instructions to calculate the target address of a change in program flow relative to the value of the program counter (PC).
Each branch instruction requires two bytes of storage — one for the branch opcode and one for the signed two’s complement 8-bit relative offset.
This offset is relative to the address of the next instruction, which is the address of the branch instruction plus two.
Consider the following line of code…
If the label HERE equates to address $1000 and this is a FORWARD branch, the target address can be between $1002 (offset of $00) and $1081 (offset of $7F).
Similarly, if the label HERE equates to address $1000 and this is a REVERSE branch, the target address can be between $0F82 (offset of $80) and $1000 (offset of $FE).
Notes:
Only branch instructions use relative addressing. A branch instruction executes in three clock cycles and consists of two bytes. The first byte is the opcode for the particular branch condition, and the second byte is a signed two’s complement offset from the address of the next instruction.
The target address of a branch instruction is calculated as…
New Program Counter = Address of Branch Opcode + 2 + Signed Offset
Because the offset for a branch is from the address of the next instruction, the smallest practical negative offset is -2 ($FE). An offset of zero causes no change in program flow, essentially making the branch instruction a three cycle NOP.