RE: 8bit binary to ASCII

From: Christopher Phillips (cphillips_at_reflectionsinteractive.com)
Date: 2001-09-26 11:40:16

> -----Original Message-----
> From: ncoplin@orbeng.com [mailto:ncoplin@orbeng.com]
> Sent: 26 September 2001 02:25
> To: cbm-hackers@cling.gu.se
> Subject: 8bit binary to ASCII
> 
> 
> Hi All,
> 
> Does any one have a short assembly routine for converting an 
> 8bit unsigned
> binary number (0-255) to ASCII?
> 

The following is untested, but it's nice and short.
I know the ((N/2*3*17)+40)/256 trick is fine, I just
don't have an assembler handy.  And my head
always hurts when deciding between bcc and bcs!

Christopher.

----------------------------------------------------------------


.convertbytetoasciidecimal
				;enter with number in A
				;exit with null terminated 3 character
string in buf

	ldx#32		;default first char to space..
	stx buf+1	;and clear low 5 bits of buf+1
	cmp#100
	bcc writefirstdigit
	sbc#100
	ldx#'1'
	cmp#100
	bcc writefirstdigit
	sbc#100
	ldx#'2'
.writefirstdigit
	stx buf

				; A now contains N=0..99
				; save it on the stack, then
				; compute ((N/2*3*17)+40)/256
				; to get the high digit
	php
	lsr a
	sta Non2
	asl a		; also clears carry :-)
	adc Non2
				;A now contains N/2*3
	sta Nby2on3
	asl a
	rol buf+1
	asl a
	rol buf+1
	asl a
	rol buf+1
	asl a
	rol buf+1	; buf+1 & A now contain hi & lo bytes of N*2/3*16
	adc Nby2on3
	bcc $1
	inc buf+1
$1:	adc#39		;doesnt matter if this adds 39 or 40
	clc
	bcc $1
	inc buf+1
$1:	clc
	lda buf+1
	tax
	adc#'0'		; and leave carry clear
	sta buf+1
	plp
	adc asczerotakeXtimes10,x
	sta buf+2
	rts

.Non2		;never need both of these at once.
.Nby2on3
	byte 0

.buf
	byte 0,0,0,0

.asczerotakeXtimes10
	byte '0'- 0, '0'-10, '0'-20, '0'-30, '0'-40
	byte '0'-50, '0'-60, '0'-70, '0'-80, '0'-90
 
-Virus scanned and cleared ok

       Message was sent through the cbm-hackers mailing list

Archive generated by hypermail 2.1.1.