INMC News

  

February/March 1980 · Issue 6

Page 23 of 38
Program hints

By N.Ray


One Or Two Notes About Z80 Machine-Code Programming

Reading other people’s Z80 code programs convinces me that many people are unaware of some very useful tricks,

For instance, although it is well-known that XOR A may be used to clear A (and the carry bit) the other operations on A have their uses.

E.g. AND A and OR A set Z and S flags from contents of A and may frequently replace CP 0.

AND A (or OR A) can also be used to clear the carry flag if you don’t mind corrupting the other flags. CP A will clear the Z flag.

A warning of general application is in order here,

AND A is not the same as CP 0
INC A is not the same as ADD A,1 (Sim. DEC)
CPL,INCA is not the same as NEG

In each case the code on the left differs from that on the right in the way it affects the carry flag (and certain other flags). While on the subject of programming dodges, here is an interesting example:

E60F   HEXASC:    AND 0FH
C690              ADD A,90H
27                DAA
CE40              ADC A,40H
27                DAA
C9                RET

The lower 4 bits in register A are interpreted as a hex digit and the corresponding hex character in ASCII is found in A on exit.

You should try to convince yourself that it works correctly (HINT : watch the half-carry flag).

Recursion

Recursion can often be used to good advantage in machine-code programming, provided there is ample stack space. A recursive routine is one that calls itself. As a demonstration, consider the ‘Tower of Hanoi’ problem.

N rings are stacked on one of three poles (as indicated)

Page 23 of 38