Page 91 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 91
68 Chapter 3 Addressing Modes
Figure 3.8. Simplified Control Hardware for Relative Addressing
program starts at location $1000 and ends at $2000. Suppose that someone buys this
ROM, but his/her microcomputer has another program in a ROM that starts at location
$1000 and ends at $2000. We would like to be able to use this new ROM so that the
new program would start at location $4000 and end at location $5000, for instance, or
wherever there is room in the address space of the microcomputer. However, because the
program is in a ROM, it cannot be changed by the buyer. Similarly, a compiler or
assembler that downloads a particular program into different parts of memory won't have
to change addresses if the program is position independent. A program that works the
same way, wherever it is loaded in memory, is said to be position independent. Position
independent programs can be written by an assembler or compiler to run anywhere in
memory without modification. Programs we have seen so far are position independent
when the location of the data is fixed, and, in fact, most program segments that do not
use JMP or JSR instructions using direct addressing are position independent.
Program counter relative addressing, or simply relative addressing, adds a two's-
complement number, called a relative offset, to the value of the program counter to get
the effective address of the operand. Figure 3.8 illustrates a simplified implementation of
a controller. The top switch can add "1," or the sign-extended data bus, to the program
counter. The former is used to increment the program counter each time an instruction
byte is fetched, and the latter is used for relative branches. The bottom switch permits the
adder's output or the data bus value to be put into the program counter. The bottom
switch selects the latter when a JMP, JSR, RTS, or RTI instruction or interrupt loads
the program counter. The adder's output can also be used as an effective address.
The relative addressing mode is used to implement position independence. If the
program segment at $1000 to $2000 was in a ROM and that ROM was installed so that
the instruction following the BNE was at $4000, the BNE instruction would still have the
relative offset $20. If Z is 0 when the instruction is executed, the program counter would
be changed to $4020. That would be the address of the instruction that had the label L,
The program would execute the same way whether it was stored at location $1000 or
$4000. This makes the program position independent.
Branching instructions all use relative addressing. For example, the instruction BRA
L for "branch always" to location L will cause the program counter to be loaded with
the address L. An example of a branch is illustrated in Figure 3.9. Observe that label L is
two bytes below the end of the BRA L instruction. The program counter PC has the
address $834 of the next instruction, LDAA #4, when it is executing the BRA L
instruction. The second byte of the BRA L instruction, the offset, 2, is added to the
program counter, to make it $836, and then the next byte is fetched.