Exchanging Registers, Part 6

Author(s): Jonathan Bobst
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v3 n6 (Volume 3, Issue 6)
Date: June 1982
Pages: 9

(Part 6 of a series)

In addition to the eight registers A,F,B,C,D,E,H and L covered in an earlier article, the Z80 (central processing unit in your ZX80/81) has a twin set of alternate registers. These registers, named A’,F’,B’,C’,D’,E’, H’ and L’, can only be accessed in their paired state (ie., AF’, BC’).

As long as your machine code routines are short, say 100 bytes of code and data, you won’t really need to use an alternate register. But sometime in your programming future it may be easier to use one, so let’s examine the method of switching information between them.

Without taking the time to examine the register pairs you will use, you can’t know whether or not your use of them will destroy vital data. So before using registers, PUSH them onto the stack to save their data. Then POP them off before returning to BASIC. This way you can use these registers during your program, but return them to their original state before exiting machine code. (Ex. PUSH HL…EXX…PUSH HL…for using both HL and HL’. Then POP HL…EXX… POP HL…before RETurning. More on PUSH and POP later.)

Only 6 instructions control exchanging registers and alternates, but only 3 are really useful in short subroutines:

EX AF,AF’ exchanges data with the alternate AF pair. EX DE,HL puts data in HL into DE and data in DE into HL. EXX exchanges data between BC,DE and HL and their alternate pairs (BC<->BC’, DE<->DE’, HL<->HL’).

For example, suppose you have two blocks of data, block 1 for use in one branch of your subroutine and block 2 for use in the other branch. One solution is to load HL with the starting address of block 1 and DE with block 2. Then, depending on a Test and Branch, HL can contain either HL or, by EXchanging DE,HL, HL can contain “DE” for an INC HL loop.

Exchanging register pairs is handy, but there is an easier way to “save” ethereal data…

Next month: PUSHing and POPping