Discussion:
Z80 Binary to Ascii
(zu alt für eine Antwort)
CTG
2004-12-31 04:46:40 UTC
Permalink
Is there any piece of code out there that would take care of converting a
Single byte , Double byte binary value into its ASCII form?
I don't want to reinvent the wheel ..

Thanks & Happy new year
Norbert Juffa
2004-12-31 09:14:09 UTC
Permalink
Post by CTG
Is there any piece of code out there that would take care of converting a
Single byte , Double byte binary value into its ASCII form?
I don't want to reinvent the wheel ..
Thanks & Happy new year
Please note that your post is off-topic. This newsgroup is about
programming x86 processors (the Z80 is not one of these), and it
is in German, not English.

If a two stage process is suitable for your conversion needs, the
code below should work.


-- Norbert


;;--------------------------------------------------
;; Binary to BCD conversion
;;
;; Converts a 16-bit unsigned integer into a 6-digit
;; BCD number. 1181 Tcycles
;;
;; input: HL = unsigned integer to convert
;; output: C:HL = 6-digit BCD number
;; destroys: A,F,B,C,D,E,H,L
;;--------------------------------------------------
Bin2Bcd:
LD BC, 16*256+0 ; handle 16 bits, one bit per iteration
LD DE, 0
cvtLoop:
ADD HL, HL
LD A, E
ADC A, A
DAA
LD E, A
LD A, D
ADC A, A
DAA
LD D, A
LD A, C
ADC A, A
DAA
LD C, A
DJNZ cvtLoop
EX HL, DE
RET

;;----------------------------------------------------
;; Converts a 6-digit BCD number to a hex ASCII string
;;
;; input: DE = pointer to start of ASCII string
;; C:HL number to be converted
;; output: DE = pointer past end of ASCII string
;; destroys: A,F,D,E
;;-----------------------------------------------------
Bcd2HexAscii:
LD A, C
CALL cvtUpperNibble
LD A, C
CALL cvtLowerNibble
LD A, H
CALL cvtUpperNibble
LD A, H
CALL cvtLowerNibble
LD A, L
CALL cvtUpperNibble
LD A, L
JR cvtLowerNibble
cvtUpperNibble:
RRA ; move upper nibble into lower nibble
RRA
RRA
RRA
cvtLowerNibble:
AND A, 0Fh ; isolate lower nibble
ADD A, 90h ; old trick
DAA ; for converting
ADC A, 40h ; one nibble
DAA ; to hex ASCII
LD (DE), A
INC DE
RET

Loading...