;;bin2asc.asm
;;Willard Goosey
;;1/7/2012

;$Id: bin2asc.asm,v 1.6 2014/11/07 07:33:03 goosey Exp goosey $

;derived from _8080/Z80 Assembly Language_
;Alan Miller, 1981

;byte2a 8bit bin to ascii number, HL preserved
;Entry Conditions: A=number
;		   HL-> 4 byte buffer
;Exit Conditions:  HL-> 4 byte buffer holds value in A as
;                       ASCII decimal chars, null terminated
;		   A destroyed

;byte2ap 8bit bin to ascii number HL incremented
;Entry Conditions: A=number
;		   HL-> 4 byte buffer
;Exit Conditions:  HL-> null terminating string of
;                       ASCII decimal chars, null terminated
;		   A destroyed

byte2a	push h
	call byte2ap
	pop h
	ret

byte2ap	push d
	push b
	mvi e,0		;leading zero flag
	mvi d,100
dec81   mvi c, '0'-1
dec82	inr c
	sub d		;100 or 10
	jnc dec82	;still +
	add d		;add back
	mov b,a		;remainder
	mov a,c		;get 100/10
	cpi '1'		;zero?
	jnc dec84	;yes
	mov a,e		;check flag
	ora a		;reset?
	mov a,c		;restore byte
	jz dec85	;leading zero
dec84	mov m,a		;*HL=a
	inx h		;HL++
	mvi e,0ffh	;set 0 flag
dec85	mov a,d
	sui 90		;100 to 10
	mov d,a
	mov a,b		;remainder
	jnc dec81	;again
	adi '0'		;ascii bias
	mov m,a		;*HL=a
	inx h		;HL++
	pop b
	pop d
	mvi m,0		;null termination
	ret
	
;end bin2asc.asm
