As Stephen Adolf discovered the Z-80 will compute overflow instead of parity when doing arithmetic operations.  Thus BASIC's 8085 RST 5 subroutine won't work on the Z-80 as the 8085 computes parity on DCR A while the Z-80 computes overflow.

Note here that the routine gets Carry from the CPI and Sign, Zero and Parity from the last DCR A.

LDA FB65H
CPI 08H
DCR A
DCR A
DCR A
RET

Adding a "OR A" will almost fix it as that will compute parity and recompute S and Z.  But that will clear the carry.  A little branching will handle this correctly:

LDA FB65H
CPI 08H
DCR A
DCR A
DCR A
JR C,has_carry
OR A
RET
has_carry:
OR A
SCF
RET

It the occurred to me that, rather like the old "replacing the step stone" joke, Microsoft had to solve this problem before.  Indeed, in the TRS-80 Model 1 ROM we see:

LD A,($40AF)
CP $08
JR NC,_25E5
SUB $3
OR A
SCF
RET
_25E5:
SUB $03
OR A
RET

I feel confident my solution will work and is even a byte shorter than what Bill or Paul (or some other Microsoft employee) came up with.

Before learning the purpose I did go into the weeds a bit looking at compact subroutines to compute parity.  I'll save that for a separate message.

                        -- George

Reply via email to