would add together all 63 bytes after clearing the A register. The
checksum is then stored at the beginning of the block in location
BLK. (A checksum such as this would be used for comparison pur-
poses on subsequent retrievals of the data block; by repeating the
add and comparing the result with the inherent checksum, the valid-
ity of the data could be established.)
SUB
A
A - A CLEARS A
LD
IX,BLK+63
SET PNTR TO END OF DATA
LD
B,63
SET COUNT TO 63
LOOP
ADD
A,(IX)
ADD NEXT BYTE
DEC
IX
INDEX - 1
DEC
B
COUNT - 1
JP
NZ,LOOP
CONTINUE IF NOT 63 BYTES
DONE
LD
(IX),A
STORE A IN BLK
BLK
DEFS
64
CHECKSUM + 63 DATA BYTES
In the above program, the last instruction LD (IX ),A stored the
checksum held in A to the location pointed to by the contents of
the index register IX. As the data started at BLK+63 and ended at
BLK+1, the index register pointed to BLK+O after the last iteration
of the loop and the checksum could be. stored without further ad-
justment to the index register.
If the block of data were to be read in from the I/O device and
the checksum to be calculated and compared, a subtract instruction
could be used to advantage.
LD
IX,INBLK+1
SET PNTR TO START OF DATA
LD
A,(IX- 1)
GET CHECKSUM
LD
B,63
SET COUNT TO 63
LOOP
SUB
(IX)
SUBTRACT NEXT BYTE
INC
IX
POINT TO NEXT BYTE
DEC
B
COUNT - 1
JP
NZ,LOOP
CONTINUE IF NOT 63 BYTES
DONE
OR
A
TEST CONTENTS OF A
JP
NZ,ERROR
GO IF ERROR IN DATA
NERROR
NO ERROR
ERROR
The IX register is first set to the start of the data at INBLK+1.
The next instruction loads the checksum byte at INBLK (IX-1 is
(INBLK+1) - 1 = INBLK) into A. Then the data at INBLK+1
through INBLK+63 is subtracted from the partial checksum in A.
At the end (DONE), the contents of A should be 0 if the data is
valid. An OR is done which simply serves to set the flags for the con-
ditional branch JP NZ,ERROR. If A is not zero, location ERROR is
162
Need help?
Do you have a question about the Z80 and is the answer not in the manual?