The following article was printed in January 1980 of the magazine „BYTE".
Description how to program a Z80 user stack.

Z80 User Stack Emulation


Allen Gelder, Box 11721 Main Post Office,
San Francisco CA 94101

Passing arguments to subroutines via the Z80 hardware stack is complicated by the presence of the subroutine return address at the top of the stack. This artifact of the CALL instruction makes a cork-in-the-bottie effect that precludes just PUSHing the arguments onto the stack and later POPing them into the subroutine after the CALL. The problem is solved on the new Motorola 6809 by the addition of a user stack which does not participate in the CALL housekeeping. Such a structure is easily emulated and can be integrated very naturally into the Z80 instruction set by the use of the restart instruction group.
Recall that the restart (RST) instructions are 1-byte calls to selected page 0 locations. For example, hexadecimal op code D7 is RST 10. When program flow encounters this instruction, the program counter (register PC) will be pushed onto the stack and control will be transferred to location 0010. Often this locatiom contains a vector to the actual routine, which will typically be concluded by a return (RET). This is the arrangement here (see listing 2, page 210). It is easy to link this emulation; just initialize UPSTOR with a 2-byte user pointer to the desired top of the user stack, and then vector the restarts as indicated. (See listing 1, page 210.)
When program flow encounters a D7 op code (RST 10), the result will be a POPU DE, that is, the top of the user stack will be popped into DE, and the user pointer (UP) will be updated. Similarly, a DF op code (RST 18) will result in a PSHU DE onto the top of the user stack, etc (see table 1). This action is perfectly consistent with current hardware stack usage, right down to the near congruence of the POPU and PSHU instruction bytes.
The difference is that this stack is totally controlled by the user, at very little programming expense. In the configuration of table 1, the user stack access covers the prirnary register pairs BC, DE, HL and AF. An alternative assignment (see listing 3, page 210, and table 2) of restarts can include registers IX and IY. This is at the expense of register pairs DE and AF, but saves one byte over the corresponding PUSH or POP IX (or IY) instruction.
Interpretation of the restart group of instructions is varied. In the literature, the restart instructions are described as saving space, useful for Interrupts, or as left-over 8080 instructions. In practice they are often usurped by the input/output (I/O) software, or perhaps page 0 is submerged in read-only memory. If this is the case in your Z80-based system, it as worth looking for a vector table in prograrnmable memory or in some other way gaining access to the restart instructions. Because they are an embedded group of 1-byte, user-programmable instructions, they bestow a kind of microprogrammability on the Z80.
User Stack Instruction         Hardware Stack Instruction

POPU   BC      C7    (RST00)       POP BC      C1
PSHU   BC      CF    (RST08)       PUSH BC     C5
POPU   DE      D7    (RST10)       POP DE      D1
PSHU   DE      DF    (RST18)       PUSH DE     D5
POPU   HL      E7    (RST20)       POP HL      E1
PSHU   HL      EF    (RST28)       PUSH HL     E5
POPU   AF      F7    (RST30)       POP AF      F1
PSHU   AF      FF    (RST38)       PUSH AF     F5
Table 1: Restart (RST) instructions assigned to user stack in listing 1.
User-Stack Instruction         Hardware-Stack Instruction

POPU   IX      D7    (RST10)       POP IX      DDE1
PSHU   IX      DF    (RST18)       PUSH IX     DDE5
POPU   IY      F7    (RST30)       POP IY      FDE1
PSHU   IY      FF    (RST38)       PUSH IY     FDE5
Table 2: Restart (RST) instruction assignments made by modified user-stack emulation in listing 2.
In this user stack setup, the restart instructions are exploited as 1-byte PSHU and POPU instructions in service of an emulated processor architectural feature. They could as easily call an emulated addressing mode not available on the Z80. The point is that an appropriate use of the restart group is in calling instruction-Iike subroutines that represent the primitives serving the user's own fanciful structure. Thus you can design your own corner of the Z80. Let your curiosity PSHU into trying it.
Listing 1: Vectoring RST10.
Listing 2: Assembled program for Z80 user-stack emulation using restart (RST) instructions.
[Z80 source]
Listing 3: Alternate assembled listing to include index registers IX and IY. These two sections of code are directly substituted for the code in listing 2.
[Z80 source]

Scanned by Werner Cirsovius
February 2013
© BYTE Publications Inc.