/ C library -- printf


.globl	_printf
.globl	_ndigit

.globl	pfloat
.globl	pscien
.globl	_putchar

.globl	rsave
.globl	rretrn

_printf:
	jsr	r5,rsave; 128.
	mov	4(r5),formp		/ format
	mov	r5,r4
	add	$6,r4			/ arglist
loop:
	movb	*formp,r0
	beq	1f
	inc	formp
	cmp	r0,$'%
	beq	2f
3:
	mov	r0,(sp)
	jsr	pc,*$_putchar
	br	loop
1:
	jmp	rretrn
2:
	clr	rjust
	clr	_ndigit
	cmpb	*formp,$'-
	bne	2f
	inc	formp
	inc	rjust
2:
	jsr	r3,gnum; width
	clr	ndfnd
	cmp	r0,$'.
	bne	1f
	jsr	r3,gnum; _ndigit
1:
	mov	sp,r3
	mov	$swtab,r1
1:
	mov	(r1)+,r2
	beq	3b
	cmp	r0,(r1)+
	bne	1b
	jmp	(r2)
swtab:
	decimal;	'd
	octal;		'o
	hex;		'x
	float;		'f
	scien;		'e
	charac;		'c
	string;		's
	logical;	'l
	0;  0

decimal:
	mov	(r4)+,r1
	bge	1f
	neg	r1
	movb	$'-,(r3)+
	br	1f

logical:
	mov	(r4)+,r1
1:
	jsr	pc,1f
	br	prbuf
1:
	clr	r0
	div	$10.,r0
	mov	r1,-(sp)
	mov	r0,r1
	beq	1f
	jsr	pc,1b
1:
	mov	(sp)+,r0
	add	$'0,r0
	movb	r0,(r3)+
	rts	pc

charac:
	movb	(r4)+,(r3)+
	bne	1f
	dec	r3
1:
	movb	(r4)+,(r3)+
	bne	prbuf
	dec	r3
	br	prbuf

string:
	mov	_ndigit,r1
	clr	r3
	mov	(r4),r2
1:
	tstb	(r2)+
	beq	1f
	inc	r3
	sob	r1,1b
1:
	mov	(r4)+,r2
	br	prstr

hex:
	mov	$1f,r2
	.data
1:
	-4; !17
	.text
	br	2f

octal:
	mov	$1f,r2
	.data
1:
	-3; !7
	.text
2:
	mov	(r4)+,r1
	clr	r0
	jsr	pc,1f
	br	prbuf
1:
	mov	r1,-(sp)
	ashc	(r2),r0
	beq	1f
	jsr	pc,1b
1:
	mov	(sp)+,r0
	bic	2(r2),r0
	add	$'0,r0
	cmp	r0,$'9
	ble	1f
	add	$'A-'0-10.,r0
1:
	movb	r0,(r3)+
	rts	pc

float:
	mov	ndfnd,r2
	jsr	pc,pfloat
	br	prbuf

scien:
	mov	ndfnd,r2
	jsr	pc,pscien
	br	prbuf

prbuf:
	mov	sp,r2
	sub	r2,r3
prstr:
	mov	r4,-(sp)
	mov	$' ,-(sp)
	mov	r3,r4
	neg	r3
	add	width,r3
	ble	1f
	tst	rjust
	bne	1f
2:
	jsr	pc,*$_putchar
	sob	r3,2b
1:
	tst	r4
	beq	2f
1:
	movb	(r2)+,(sp)
	jsr	pc,*$_putchar
	sob	r4,1b
2:
	tst	r3
	ble	1f
	mov	$' ,(sp)
2:
	jsr	pc,*$_putchar
	sob	r3,2b
1:
	tst	(sp)+
	mov	(sp)+,r4
	jmp	loop

gnum:
	clr	ndfnd
	clr	r1
1:
	movb	*formp,r0
	inc	formp
	sub	$'0,r0
	cmp	r0,$9.
	bhi	1f
	inc	ndfnd
	mul	$10.,r1
	add	r0,r1
	br	1b
1:
	add	$'0,r0
	mov	r1,*(r3)+
	rts	r3

.bss
width:	.=.+2
formp:	.=.+2
rjust:	.=.+2
ndfnd:	.=.+2
