Page 94 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 94
3.4 Stack Index Addressing, Reentrancy and Recursion 71
A program is not position independent if any instruction in it causes it to do
something different when the program is moved, intact, to a different location. The only
real test for a program's position independence is to show that it can be moved without
changing its operation. One necessary condition, however, is that all changes to the
program counter be position independent, and using branch instructions in place of jump
instructions, or JMP and JSR instructions with program counter relative addressing, will
generally make that possible. The relative addressing mode is generally used with data
that move with the program, such as constants that are on the same ROM as the
program, and with instructions that compute the address to jump to in a manner to be
introduced later. Listed with other instructions, then, the relative mode allows programs
to be position independent, and that may be very important in a microcomputer that uses
a lot of ROMs.
3.4 Stack Index Addressing, Reentrancy, and Recursion
The stack pointer may be used with all the index addressing modes, but its uses have
special meaning. These uses correspond to pushing and pulling, and they support
reentrancy and recursion. Also, the index registers X and Y may be used as auxiliary
stack pointers. This section shows these variations of index addressing.
The instruction LDAA 1, SP+ is essentially the same as PULA because both pull a
byte from the (hardware) stack into accumulator A. Similarly, the instruction LDD
2 1 SP+ is essentially the same as PULD; the instruction STAA 1, -SP is essentially
the same as PSHA; and the instruction STD 2,-SP is essentially the same as
PSHD. The differences between these pairs of instructions is in their length and
execution time and in the effect they have on condition codes. The instructions PULA,
PSHA, PULD, and PSHD are usually preferred because they are faster and shorter, but
the instructions LDAA 1,SP+, STD 2,-SP, and so on may be used if pulling or
pushing needs to set the condition codes for a future conditional branch instruction.
Moreover, autoincrement addressing can be used with other instructions to pull a
byte or 16-bit word and simultaneously use the pulled data in an instruction. The
sequence:
PSHB
ADDA 1,SP+
is often used in code generated by C and C++ compilers to add accumulator B to
accumulator A (equivalent to the simpler instruction ABA). However, this push-and-pull-
into-an-instruction technique can be used with other instructions like ANDA and ADDD.
The sequence:
PSHX
ADDD 2,SP+
is often used in code generated by C and C++ compilers to add index register X to
accumulator D.