RE: 8bit binary to ASCII

ncoplin_at_orbeng.com
Date: 2001-09-26 12:27:17

Thanks Guys,
There's obviously several ways to do it, will check through the merits of
each.

-----Original Message-----
From: Christopher Phillips [mailto:cphillips@reflectionsinteractive.com]
Sent: Wednesday, 26 September 2001 5:40 PM
To: 'cbm-hackers@cling.gu.se'
Subject: RE: 8bit binary to ASCII

> 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


PLEASE TAKE NOTE:

The contents of this email (including any attachments) may be
privileged and confidential. Any unauthorised use of the contents
is expressly prohibited. If you have received this email in error,
please advise us immediately (you can contact us by telephone
on +61 8 9441 2311 by reverse charge) and then permanently
delete this email together with any attachments. We appreciate
your co-operation.

Whilst Orbital endeavours to take reasonable care to ensure
that this email and any attachments are free from viruses or other
defects, Orbital does not represent or warrant that such is explicitly
the case

(C) 2000: Orbital Engine Company (Australia) PTY LTD and its
affiliates


       Message was sent through the cbm-hackers mailing list

Archive generated by hypermail 2.1.1.