32 bit long to ascii

Page 2/2
1 |

By ARTRAG

Enlighted (6923)

ARTRAG's picture

10-12-2009, 13:51

From your description I assume that this could work.
(untested code !!)

	
;     * Addition:
;       Input: BC = base address of addend+5, DE = base address of adder+5
;       Output: Addend replaced by addend plus adder
_BCDadd:
	ld		h,b
	ld		l,c
	and		a
	
	rept	5		; 10 BCD digits
	ld      a, (de)	;get byte of adder
	adc     a, (hl)	;add it to addend, care for carry
	daa				;convert to BCD-decimal
	ld      (de), A	;save number back in addend
	dec     hl		;next number
	dec     de
	endm			;continue until all bytes summed
	ret

;     * Subtraction:
;       Works nearly the same, but instead of the ADC, a SBC is used.
;       Input: BC = base address of minued+5, DE = base address of subtrahend+5
;       Output: Minuend replaced by minuend minus subtrahend
_BCDsub:
	ld		h,b
	ld		l,c
	and		a
	ex      de,hl	;exchange subtrahend and minuend
	
	rept	5		; 10 BCD digits
	ld      a, (de)	;get byte of minuend
	sbc     a, (hl)	;subtract byte of subtrahend
	daa				;convert to BCD-decimal
	ld      (de), A	;save number back in minuend
	dec     hl		;next number
	dec     de
	endm			;continue until all bytes summed
	ret

By RetroTechie

Paragon (1563)

RetroTechie's picture

11-12-2009, 00:40

code from this site seemd broken
http://icarus.ticalc.org/articles/z80_faq.html

as no carry is passed between digits in different bytes
Seems fine to me... in both snippets, loop is entered with carry reset (or A), and carry is passed between bytes (adc, sbc). The other loop instructions (LD, INC/DEC rr, DJNZ) should preserve the carry between passes.

By RetroTechie

Paragon (1563)

RetroTechie's picture

11-12-2009, 00:51

Btw. found it...
In: bit 0-3 of A = 0-F hex digit to print

        OR #F0
        DAA
        ADD A,#A0
        ADC A,#40 

Out: A = ASCII code "0"-"9" or "A"-"F" depending on input

Absolutely brilliant code! (not mine, just found it somewhere in a book or on the 'net). Exercise for the reader: see if it's possible to modify such that ASCII output is in lower case... BA-team

By Prodatron

Paragon (1836)

Prodatron's picture

11-12-2009, 08:51

Yes, indeed brilliant code! (wasn't it MSDs one?)

By ARTRAG

Enlighted (6923)

ARTRAG's picture

11-12-2009, 11:33

code from this site seemd broken
http://icarus.ticalc.org/articles/z80_faq.html

as no carry is passed between digits in different bytes
Seems fine to me... in both snippets, loop is entered with carry reset (or A), and carry is passed between bytes (adc, sbc). The other loop instructions (LD, INC/DEC rr, DJNZ) should preserve the carry between passes.

My fault, I was expecting carry was changed by djnz.

Thanks!

By ARTRAG

Enlighted (6923)

ARTRAG's picture

10-04-2010, 00:24

@RetroTechie

BCD code on
http://icarus.ticalc.org/articles/z80_faq.html
is working

You are rigth.

Page 2/2
1 |