Section 3 Addressing Modes: Appendix A In-Struction Reference Table 3-1
Section 3 Addressing Modes: Appendix A In-Struction Reference Table 3-1
ADDRESSING MODES
Addressing modes determine how the CPU accesses memory locations to be operat-
ed upon. This section discusses the various modes and how they are used.
Examples:
These are common examples of 8-bit and 16-bit immediate addressing mode. The
size of the immediate operand is implied by the instruction context. In the third exam-
ple, the instruction implies a 16-bit immediate value but only an 8-bit value is supplied.
In this case the assembler will generate the 16-bit value $0067 because the CPU ex-
pects a 16-bit value in the instruction stream.
This is a very basic example of direct addressing. The value $55 is taken to be the
low-order half of an address in the range $0000 through $00FF. The high order half of
the address is assumed to be zero. During execution of this instruction, the CPU com-
bines the value $55 from the instruction with the assumed value of $00 to form the ad-
dress $0055, which is then used to access the data to be loaded into accumulator A.
In this example, the value $20 is combined with the assumed value of $00 to form the
address $0020. Since the LDX instruction requires a 16-bit value, a 16-bit word of data
is read from addresses $0020 and $0021. After execution of this instruction, the X in-
dex register will have the value from address $0020 in its high-order half and the value
from address $0021 in its low-order half.
Example:
This is a very basic example of extended addressing. The value from address $F03B
is loaded into the A accumulator.
Bit-condition branches test whether bits in a memory byte are in a specific state. Var-
ious addressing modes can be used to access the memory location. An 8-bit mask op-
erand is used to test the bits. If each bit in memory that corresponds to a one in the
mask is either set (BRSET) or clear (BRCLR), an 8-bit offset is added to the address
of the next memory location after the offset to form an effective address, and execution
continues at that address; if all the bits in memory that correspond to a one in the mask
are not in the specified state, execution continues with the instruction immediately fol-
lowing the branch instruction.
Both 8-bit and 16-bit offsets are signed two’s complement numbers to support branch-
ing upward and downward in memory. The numeric range of short branch offset val-
ues is $80 (–128) to $7F (127). The numeric range of long branch offset values is
$8000 (–32768) to $7FFF (32767). If the offset is zero, the CPU executes the instruc-
tion immediately following the branch instruction, regardless of the test involved.
Since the offset is at the end of a branch instruction, using a negative offset value can
cause the PC to point to the opcode and initiate a loop. For instance, a branch always
(BRA) instruction consists of two bytes, so using an offset of $FE sets up an infinite
loop; the same is true of a long branch always (LBRA) instruction with an offset of
$FFFC.
An offset that points to the opcode can cause a bit-condition branch to repeat execu-
tion until the specified bit condition is satisfied. Since bit condition branches can con-
sist of four, five, or six bytes depending on the addressing mode used to access the
byte in memory, the offset value that sets up a loop can vary. For instance, using an
offset of $FC with a BRCLR that accesses memory using an 8-bit indexed postbyte
sets up a loop that executes until all the bits in the specified memory byte that corre-
spond to ones in the mask byte are cleared.
All indexed addressing modes use a 16-bit CPU register and additional information to
create an effective address. In most cases the effective address specifies the memory
location affected by the operation. In some variations of indexed addressing, the ef-
fective address specifies the location of a value that points to the memory location af-
fected by the operation.
Examples:
For these examples, assume X has a value of $1000 and Y has a value of $2000 be-
fore execution. The 5-bit constant offset mode does not change the value in the index
register, so X will still be $1000 and Y will still be $2000 after execution of these in-
structions. In the first example, A will be loaded with the value from address $1000. In
the second example, the value from the B accumulator will be stored at address $1FF8
($2000 – $8).
For these examples assume X is $1000 and Y is $2000 before execution of these in-
structions. (These instructions do not alter the index registers so they will still be $1000
and $2000 respectively after the instructions.) The first instruction will load A with the
value from address $10FF and the second instruction will load B with the value from
address $1FEC.
This variation of the indexed addressing mode in the CPU12 is similar to the M68HC11
indexed addressing mode, but is functionally enhanced. The M68HC11 CPU provides
for unsigned 8-bit constant offset indexing from X or Y, and use of Y requires an extra
instruction byte and thus, an extra execution cycle. The 9-bit signed offset used in the
CPU12 covers the same range of positive offsets as the M68HC11, and adds negative
offset capability. The CPU12 can use X, Y, SP or PC as the base index register.
When an LEAS, LEAX, or LEAY instruction is executed using this addressing mode,
and the operation modifies the index register that is being loaded, the final value in the
register is the value that would have been used to access a memory operand (premod-
ification is seen in the result but postmodification is not).
Examples:
For a “last-used” type of stack like the CPU12 stack, these four examples are equiva-
lent to common push and pull instructions. For a “next-available” stack like the
M68HC11 stack, PSHA is equivalent to STAA 1,SP– and PULA is equivalent to LDAA
1,+SP. However, in the M68HC11, 16-bit operations like PSHX and PULX require mul-
tiple instructions to decrement the SP by one, then store X, then decrement SP by one
again.
Example:
This example demonstrates how to work with data structures larger than bytes and
words. With this instruction in a program loop, it is possible to move words of data from
a list having one word per entry into a second table that has four bytes per table ele-
ment. In this example the source pointer is updated after the data is read from memory
(post-increment) while the destination pointer is updated before it is used to access
memory (pre-increment).
Example:
This instruction internally adds B to X to form the address from which A will be loaded.
B and X are not changed by this instruction. This example is similar to the following
two-instruction combination in an M68HC11.
However, this two-instruction sequence alters the index register. If this sequence was
part of a loop where B changed on each pass, the index register would have to be re-
loaded with the reference value on each loop pass. The use of LDAA B,X is more ef-
ficient in the CPU12.
PC-relative addressing uses the address of the location immediately following the last
byte of object code for the current instruction as a reference point. The CPU12 normal-
ly corrects for queue offset and for instruction alignment so that queue operation is
transparent to the user. However, move instructions pose three special problems:
1. Some moves use an indexed source and an indexed destination.
2. Some moves have object code that is too long to fit in the queue all at one time,
so the PC value changes during execution.
3. All moves do not have the indexed postbyte as the last byte of object code.
These cases are not handled by automatic queue pointer maintenance, but it is still
possible to use PC-relative indexing with move instructions by providing for PC offsets
in source code.
Table 3-3 shows PC offsets from the location immediately following the current in-
struction by addressing mode.
Example:
The BCLR and BSET instructions use an 8-bit mask to determine which bits in a mem-
ory byte are to be changed. The mask must be supplied with the instruction as an im-
mediate mode value. The memory location to be modified can be specified by means
of direct, extended, or indexed addressing modes.
The BRCLR and BRSET instructions use an 8-bit mask to test the states of bits in a
memory byte. The mask is supplied with the instruction as an immediate mode value.
The memory location to be tested is specified by means of direct, extended, or indexed
addressing modes. Relative addressing mode is used to determine the branch ad-
dress. A signed 8-bit offset must be supplied with the instruction.
Increased code efficiency is the greatest advantage of using a switching scheme in-
stead of a large linear address space. In systems with large linear address spaces, in-
structions require more bits of information to address a memory location, and CPU
overhead is greater. Other advantages include the ability to change the size of system
memory and the ability to use various types of external memory.
However, the add-on bank switching schemes used in other microcontrollers have
known weaknesses. These include the cost of external glue logic, increased program-
ming overhead to change banks, and the need to disable interrupts while banks are
switched.
The M68HC12 system requires no external glue logic. Bank switching overhead is re-
duced by implementing control logic in the MCU. Interrupts do not need to be disabled
during switching because switching tasks are incorporated in special instructions that
greatly simplify program access to extended memory.
MCUs with expanded memory treat the 16 Kbytes of memory space from $8000 to
$BFFF as a program memory window. Expanded-memory devices also have an 8-bit
program page register (PPAGE), which allows up to 256 16-Kbyte program memory
pages to be switched into and out of the program memory window. This provides for
up to 4 Megabytes of paged program memory.
The CPU12 instruction set includes CALL and RTC (return from call) instructions,
which greatly simplify the use of expanded memory space. These instructions also ex-
ecute correctly on devices that do not have expanded-memory addressing capability,
thus providing for portable code.
The CALL instruction is similar to the JSR instruction. When CALL is executed, the
current value in PPAGE is pushed onto the stack with a return address, and a new in-
struction-supplied value is written to PPAGE. This value selects the page the called
subroutine resides upon, and can be considered to be part of the effective address.
For all addressing mode variations except indexed indirect modes, the new page value
is provided by an immediate operand in the instruction. For indexed indirect variations
of CALL, a pointer specifies memory locations where the new page value and the ad-
dress of the called subroutine are stored. Use of indirect addressing for both the page
value and the address within the page frees the program from keeping track of explicit
values for either address.
The RTC instruction restores the saved program page value and the return address
from the stack. This causes execution to resume at the next instruction after the orig-
inal CALL instruction.