RE: 8bit binary to ASCII

From: Christopher Phillips (
Date: 2001-09-26 13:43:06

> -----Original Message-----
> From: []
> Sent: 26 September 2001 11:27
> To:
> Subject: RE: 8bit binary to ASCII
> Thanks Guys,
> There's obviously several ways to do it, will check through 
> the merits of
> each.

No worries.

And here's a somewhat less buggy version of the code I sent
earlier today.. (removed duplicate lables, delete a CLC
that shouldn't have been there).  It's marginally faster
and shorter as well (inx instead of inc buf+1)

				;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
	bcc writefirstdigit
	bcc 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
	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
	ldx buf+1	; X & A now contain hi & lo bytes of N*2/3*16
	adc Nby2on3
	bcc $1
	inx	      ; X & A now contain hi & lo bytes of N*2/3*17
$1:	adc#39		;doesnt matter if this adds 39 or 40
	bcc $2
$2:	clc
	adc#'0'		; and leave carry clear
	sta buf+1
	adc asczerotakeXtimes10,x
	sta buf+2

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

	byte 0,0,0,0

	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.