Page 53 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 53
30 Chapter 2 The Instruction Set
Figure 2.2. A Stack
and is thereafter adjusted only by push and pull instructions and, perhaps, the LEAS
instruction to move it. For example, at the beginning of the program, the instruction
LDS #$cOO initializes the stack so that the first push is into location $bff.
If a byte is pushed onto the stack, SP is decremented by 1, and a byte, from one of
the 8-bit registers, is put into location (SP). If one byte is removed or pulled from the
stack, the byte is transferred to one of the 8-bit registers, and SP is incremented by 1. If
two bytes are pushed onto the stack, SP is decremented by 2, and two bytes, from one of
the 16-bit registers, are put into location (SP) and (SP+1). If two bytes are pulled from
the stack, two bytes from location (SP) and (SP+1) are put into one of the 16-bit
registers and SP is incremented by 2. Any of the registers, except SP, may be pushed or
pulled from the stack for which SP is the stack pointer. PSHB will push B onto the
stack, PULD will pull two words from the stack, putting the combined word in
accumulator D. The order for 16-bit words is always that the low byte is pushed before
the high byte and the high byte pulled before low byte.
The stack will later be used for saving and restoring the program counter when
executing a subroutine and saving and restoring all the registers when executing an
interrupt. It will later also be used to store procedure arguments and local variables.
The hardware stack and the stack pointer SP must be used with some care in
computers like the 6812. There may be a temptation to use it as an index register, to
move it around to different data locations. This is very dangerous. Interrupts, which may
occur randomly, save data on the hardware stack, and programs used to aid in the testing
and debugging of your program generally use interrupts. Such a program may be very
Table 22. Stack Move Instructions