Using the stack pointer (r15), D the, Using – Zilog Z16F2810 User Manual
Page 52: The stack pointer (r15)
Operand Addressing
UM018809-0611
36
ZNEO
®
CPU Core
User Manual
Using the Stack Pointer (R15)
Stack operations are a special kind of register-indirect memory access. The ZNEO CPU
system stack is implemented using ALU Register R15 as the Stack Pointer (SP). R15 can
be addressed like any register, but because of its Stack Pointer role it would be awkward to
use for any other purpose. The ZNEO CPU assembler recognizes SP as a synonym for
R15.
The system program startup routine initializes R15 to point to the highest address in inter-
nal RAM, plus 1. Subsequent PUSH, PUSHM, CALL, and LINK instructions; interrupts,
system exceptions, and traps all decrement SP before they store data on the stack. POP,
POPM, RET, UNLINK, and IRET instructions all increment SP to release stack space as it
is no longer needed. A program can also allocate or release stack space by changing the
register R15 (SP) value directly.
A system exception is provided to help keep the stack from overwriting other data; see the
section on page 50. Software can use the PUSH, POP, PUSHM, and
POPM instructions to store and retrieve data from the stack.
PUSH decrements SP and stores the source value onto the stack. POP loads the last value
on the stack into the specified register and increments SP. The assembler uses predecre-
ment and postincrement LD opcodes to implement most PUSH and POP instructions.
PUSH and POP can be used with 8-, 16-, or 32-bit data sizes. 8-bit and 16-bit POP instruc-
tions can be either Unsigned or Signed.
When a 16-bit or 32-bit value is pushed onto the stack, the low-order bytes are pushed first
to store the value in the ZNEO CPU’s normal big-endian fashion.
Example
A 16-bit value is stored with bits [7:0] in the value’s higher-addressed byte, and bits [15:8]
in the value’s base address byte.
If the stack is located on a 16-bit bus, an assembly language program might improve stack
performance by maintaining an even SP value—for example, by avoiding the single-byte
PUSH.B and POP.B instructions. This may require some effort, especially if the program
includes compiled C routines or any other code that does not preserve stack alignment.
The PUSHM and POPM instructions push or pop multiple registers with a single instruc-
tion. For example, the following statements push R0, R5, R6, R7 and R13 onto the stack
(in reverse numerical order), and then pop the same registers (in numerical order, so
pushes and pops remain symmetrical):
PUSHM
POPM