MC Programming: PUSHing and POPping (Part 7)

Author(s): Jonathan Bobst
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v3 n7 (Volume 3, Issue 7)
Date: July 1982
Pages: 15

(Part 7 of a series)

In the 4K (ZX80) ROM, the System Variables are at addresses 16384-17423. When processing input or program lines, the Z80A central processor uses the IY register pair “indexed” at 16384. In other words, the system variables area is IY’s storage for control data.

While you can use any RAM address for data storage, avoid these addresses:

1- 16384-16423 System Variables
2- An address-pair that must hold a
   line number for a statement
   (such as 16424-16425--first
   program line)
3- Any address-pair to be
   displayed.

ZX80s won’t accept a statement without a “function” so the only place left to store data and machine code subroutines is in REM lines, preferably at line 1.

However, addresses are not the only place to store data. Registers hold data without needing addresses but only 10 pairs are available, so the Z80 keeps register-stored data in the Stack.

This Last In-First Out “box” is an ideal place to hold transient data during processing. But while it is the easiest storage area to use (only two 1-byte instructions to access it), it is also the easiest tool to crash your MC program.

Unfortunately, ZX80 already stores data in the registers you’ll want to use. If you “erase” certain data…ZAP! Scrambled screen.

Solution: PUSH a register pair before you use it and POP it before RETurning to BASIC. PUSH means put data onto the Stack; POP means pull it back off. The commands for these functions are of the form PUSH qq and POP qq where qq is a register pair (IY, IX or another). Check Appendix A of the ZX81 manual or a Z80 programming book for codes. Three rules:

1- PUSH the register pair before
   you use it.
2- POP the data back into the
   correct register pair before
   RETurning to BASIC (C9).
3- Make sure you have one POP for
   each PUSH: don't push more data
   than you will POP, and don't POP
   more data than you have PUSHed.

Remember the Last In-First Out rule: each time you PUSH data, it goes on the top of the Stack. When you POP it, it comes off the top of the Stack.

Next month: CP TESTS FOR BRANCHING


CORRESPONDING ADDRESSES FOR ZX81:

Systems Variables 16384-16508
First BASIC line No. 16509-16510

Also, Sinclair warns against using A’,F’,IX,IY, and R register when using SLOW mode. When you return to BASIC, you must be sure IY=4000h and I=001Eh.–KO