3
2.3 Stack Pointer (S)
The Stack Pointer is an eight-bit register used for generating interrupts and calling subroutines.
When an interrupt is received, the following procedure is performed automatically in the
indicated sequence:
(1) The contents of the high-order eight bits of the Program Counter (PC
H
) are saved to
an address using the Stack Pointer contents for the low-order eight bits of the address.
(2) The Stack Pointer contents are decremented by 1.
(3) The contents of the low-order eight bits of the Program Counter (PC
L
) are saved to an
address using the Stack Pointer Contents for the low-order eight bits of the address.
(4) The Stack Pointer contents are decremented by 1.
(5) The contents of the Processor Status Register (PS) are saved to an address using the
Stack Pointer contents for the low-order eight bits of the address.
(6) The Stack Pointer contents are decremented by 1.
The Processor Status Register is not saved when calling subroutines (items (5) and (6) above
are not executed). The Processor Status Register is saved by executing the PHP instruction
in software.
To prevent data loss when generating interrupts and calling subroutines, it is necessary to
save other registers as well. This is done by executing the proper instruction in software while
in the interrupt service routine or subroutine.
The high-order eight bits of the address are determined by the Stack Page Selection Bit.
For example, the PHA instruction is executed to save the contents of the Accumulator.
Executing the PHA instruction saves the Accumulator contents to an address using the Stack
Pointer contents as the low-order eight bits of the address.
The RTI instruction is executed to return from an interrupt routine.
When the RTI instruction is executed, the following procedure is performed automatically in
sequence.
(1) The Stack Pointer contents are incremented by 1.
(2) The contents of an address using the Stack Pointer contents as the low-order eight bits
of the address is returned to the Processor Status Register (PS).
(3) The Stack Pointer contents are incremented by 1.
(4) The contents of an address using the Stack Pointer as the low-order eight bits of the
address is returned to the low-order eight bits of the Program Counter (PC
L
).
(5) The Stack Pointer contents are incremented by 1.
(6) The contents of an address using the Stack Pointer as the low-order eight bits of the
address is returned to the high-order eight bits of the Program Counter (PC
H
).
Steps (1) and (2) are not performed when returning from a subroutine using the RTS
instruction. The Processor Status Register should be restored before returning from a
subroutine by using the PLP instruction. The Accumulator should be restored before returning
from a subroutine or an interrupt servi
cing routine by using the PLA instruction.
The PLA and PLP instructions increment the Stack Pointer by 1 and return the contents of an
address stored in the Stack Pointer to the Accumulator or Processor Status Register, respec-
tively.
☞
Saving data in the stack area gradually fills the RAM area with saved data; therefore,
caution must exercised concerning the depth of interrupt levels and subroutine nesting.
CENTRAL PROCESSING UNIT
Stack Pointer (S)