[A83] Re: HL to string


[Prev][Next][Index][Thread]

[A83] Re: HL to string




Here are the routines. Note that they aren't quite as efficient as they
could be, since they track more BCD numbers than necessary. Theoretically
they're running twice as slow as they could; but to fix this problem would
also take some speed, so the speed loss isn't *too* significant. Use
something like:

	ld	hl,12345
	call	Bin16ToBCD
	ld	hl,TEMP+5	;this can be whatever other temporary memory you may have
	call	BCDToStr
	bcall	vPutS

Note that you could instead do a loop with vPutMap instead, but since you're
bcall'ing it each time, it would be a bit slower, I think.

#### Don't forget to preclear the first part of TEMP that the Bin*toBCD
routines use ####

;----- Begin Routines -----

;Input: DE->BCD representation of a number
;       HL->location to output string
;       B=number of bytes in this BCD number
;Output: HL->output string
function(BCDToStr):
	push	hl
@zero	ld	a,[de]
	inc	de
	dec	b
	jr	z,@done
	or	a
	jr	z,@zero
@done	dec	de
	inc	b
	ld	a,[de]
	and	$F0
	jr	z,@skip
@loop	ld	a,[de]
	rrca
	rrca
	rrca
	rrca
	and	$0F
	add	a,'0'
	ld	[hl],a
	inc	hl
@skip	ld	a,[de]
	and	$0F
	add	a,'0'
	ld	[hl],a
	inc	hl
	inc	de
	djnz	@loop
	ld	[hl],0
	pop	hl
	ret

;Input: 16-bit number in HL
;       Change TEMP to a temporary location with 3 bytes free
;Output: DE->TEMP, which has the BCD representation of HL
function(Bin16ToBCD):
	ld	c,17
@loop	ld	b,3
	dec	c
	ret	z
	ld	de,TEMP+3
	add	hl,hl
@daa	dec	de
	ld	a,[de]
	adc	a,a
	daa
	ld	[de],a
	djnz	@daa
	jp	@loop

;Input: 32-bit number in HLIX (in that order of significance)
;       Change TEMP to a temporary location with 5 bytes free
;       (these bytes need to be zeroed out before each use!)
;Output: DE->TEMP, which has the BCD representation of HLIX
function(Bin32ToBCD):
	ld	c,33
@loop	ld	b,5
	dec	c
	ret	z
	ld	de,TEMP+5
	add	ix,ix
	adc	hl,hl
@daa	dec	de
	ld	a,[de]
	adc	a,a
	daa
	ld	[de],a
	djnz	@daa
	jp	@loop




Follow-Ups: References: