38
www.ubicom.com
IP2022 Data Sheet
4.4
Subroutine Call/Return Stack
A 16-level hardware call/return stack is provided for
saving the program counter on a subroutine call and
restoring the program counter on subroutine return. The
stack is not mapped into the data memory address space
except for the top level, which is accessible as the CALLH
and CALLL registers. Software can read and write these
registers to implement a deeper stack, in those cases
which require nesting subroutines more than 16 levels
deep. This stack is completely independent of the stack
used with the push and pop instructions and the
SPH/SPL register pair.
Note: The CALLL and CALLH registers require special
attention as modification of these values (the top of the
stack) changes the return vector.
When a subroutine is called, the return address is pushed
onto the subroutine stack, as shown in Figure 4-12.
Specifically, each saved address in the stack is moved to
the next lower level to make room for the new address to
be saved. Stack 1 receives the contents of the program
counter. Stack 16 is overwritten with what was in Stack 15.
The contents of stack 16 are lost.
Figure 4-12 Stack Operation on Subroutine Call
When a return instruction is executed the subroutine stack
is popped, as shown in Figure 4-13. Specifically, the
contents of Stack 1 are copied into the program counter
and the contents of each stack level are moved to the next
higher level. When a value is popped off the stack, the
bottom entry is initialized to 0xFFFF. For example, Stack
1 receives the contents of Stack 2, etc., until Stack 15 is
overwritten with the contents of Stack 16. Stack 16 is
initialized to 0xFFFF.
Figure 4-13 Stack Operation on Subroutine Return
For program bugs involving stack underflow, the
instruction at byte address 0x1FFFE (word address
0xFFFF) can be used to jump to an appropriate handler.
For example, system recovery may be possible by
jumping to the reset vector at byte address 0x1FFE0
(word address 0xFFF0).
The options for returning from a CALL are:
1. RET - The stack will be popped (CALLH/L will be load-
ed into PCH/L) and the page bits (PA2:0 in the STA-
TUS register) will be loaded with the upper 3 bits of
CALLH.
2. RETNP - Same as above, but PA2:0 are not changed.
3. RETW #lit - Same as RET, but also moves literal to W.
515-010a.eps
Stack 1
Stack 2
Program Counter (15:0)
Stack 16 Contents
are Discarded
Stack 3
Stack 4
Stack 5
Stack 6
Stack 7
Stack 8
Stack 9
Stack 10
Stack 11
Stack 12
Stack 13
Stack 14
Stack 15
Stack 16
CALLH/CALLL
515-011.eps
Stack 1
Stack 2
Program Counter (15:0)
Stack 16 Contents
Loaded with 0xFFFF
Stack 3
Stack 4
Stack 5
Stack 6
Stack 7
Stack 8
Stack 9
Stack 10
Stack 11
Stack 12
Stack 13
Stack 14
Stack 15
Stack 16