Download Print this page

ZiLOG Z80 Handbook page 232

Hide thumbs Also See for Z80:

Advertisement

The next routine in this set converts an 8-bit value in the C regis-
ter, assumed to be two bcd digits, to two ASCII decimal digits 0-9.
On entry, the HL register pair points to the buffer area and on exit,
HL points to buffer + 2.
BXBCD LD A,FOH MASK
AND A,C GET FIRST BCD DIGIT
R RCA
RRCA
R RCA
RRCA ALIGN FOR CONVERT
ADD A,30H CONVERT TO 0-9
LD (HL),A STORE
INC HL
LD A,FH
AND A,C GET SECOND BCD DIGIT
ADD A,30H CONVERT TO 0-9
LD (HL),A STORE
INC HL
RET RETURN
The last routine converts a 16-bit binary value in the HL register
to five ASCII characters 0-9. On entry, IX points to the start of the
character buffer; on exit, IX points to the start of the buffer + 5. To
avoid a 16 by 8 divide with a remainder, use is made of a table
lookup to find powers of ten. The table consists of 104, 103, 102, 101,
and units, and is indexed by the current iteration count of the loop.
A successive subtraction of the power of ten is performed to find the
number of times each of the powers of ten will "go into" the residue.
The number of times each can successfully be subtracted is the deci-
mal digit for that power of ten.
BXDEC
LD
IY,P10TAB
POWER OF TEN TABLE
LOOPO
XOR
A
SET DIGIT CNT 0
LD
D,(IY)
LD
E,(IY + 1)
LOAD - POWER OF TEN
LOOP]
OR
A
CLEAR CARRY
SBC
HL,DE
SUBTRACT POWER OF 10
JP
NC,JUMP1
GO IF DONE
INC
A
BUMP DIGIT COUNT
JP
LOOP1
JUMP1
ADD
HL,DE
RESTORE TO POSITIVE
LD
(IX),A
STORE DIGIT COUNT
INC
IX
BUMP BUFFER POINTER
INC
IY
INC
IY
POINT TO NEXT POWER OF 10
CP
E,1
TEST FOR 5 DIGITS
JP
NZ,LOOPO
OUTER LOOP
RET
RETURN
241

Advertisement

loading
Need help?

Need help?

Do you have a question about the Z80 and is the answer not in the manual?

Questions and answers