/ 
/ Long to ascii.
/ A subroutine of `printf'.
/

	.globl	__ltoa

/
/ Double precision power of
/ ten table. Used by the decimal
/ conversion.
/

tab:	052013;	162000 
	035632;	145000 
	002765;	160400 
	000230;	113200 
	000017;	041100 
	000001;	103240 
	000000;	023420 
	000000; 001750
	000000; 000144
	000000; 000012
tabe:

/
/ __ltoa(b, r, l);
/ char *b; int r; long l;
/ The long integer `l' is converted from
/ binary to ascii and the result stored
/ in the buffer `b'. The argument `r'
/ determines the conversion radix; it is
/ one of `d' (decimal), `o' (octal), `x'
/ (hexadecimal with 10 thru 15 in lower
/ case), `X' (hexadecimal with 10 thru
/ 15 in upper case) or `u' (unsigned).
/

__ltoa:	jsr	r0,__csav

	mov	12(r5),r4
	mov	16(r5),r2
	mov	20(r5),r3

	clr	-(sp)

	cmp	14(r5),$'d
	beq	3f
	cmp	14(r5),$'u
	beq	4f

/
/ Octal.
/ Hexadecimal.
/

	mov	$8.,r1
	cmp	14(r5),$'o
	bne	2f
	mov	$11.,r1
	clr	r0
	br	0f

2:	clr	r0
	asl	r3
	rol	r2
	rol	r0

0:	asl	r3
	rol	r2
	rol	r0
	asl	r3
	rol	r2
	rol	r0

	cmp	14(r5),$'o
	beq	0f
	asl	r3
	rol	r2
	rol	r0

0:	cmp	r1,$1
	beq	0f
	tst	r0
	bne	0f
	tst	(sp)
	beq	1f

0:	inc	(sp)
	add	$'0,r0
	cmp	r0,$'9
	blos	0f
	add	$'A-'0-10.,r0
	cmp	14(r5),$'X
	beq	0f
	add	$'a-'A,r0

0:	movb	r0,(r4)+

1:	dec	r1
	bne	2b
	br	5f

/
/ Decimal.
/ Unsigned.
/

3:	tst	r2
	bpl	4f
	movb	$'-,(r4)+
	neg	r2
	neg	r3
	sbc	r2

4:	mov	$tab,r1

2:	clr	r0

1:	cmp	r2,(r1)
	blo	1f
	bhi	0f
	cmp	r3,2(r1)
	blo	1f

0:	sub	(r1),r2
	sub	2(r1),r3
	sbc	r2
	inc	r0
	br	1b

1:	tst	r0
	bne	0f
	tst	(sp)
	beq	1f

0:	inc	(sp)
	add	$'0,r0
	movb	r0,(r4)+

1:	add	$4,r1
	cmp	r1,$tabe
	blo	2b

	add	$'0,r3
	movb	r3,(r4)+

5:	clrb	(r4)
	jmp	__cret
