	.title	$itoc
	.ident	/x01/
 
	.enabl	lc
	.nlist	bex
 
;
; Utility Routines
; Integer to Character
;
; Version x01
;
; David G. Conroy	12-Dec-77
;
 
	.globl	$itoc
 
	.mcall	call
	.mcall	callr
	.mcall	return
 
;+
; ** $itoc - integer to character
;
; This routine depends  on the fact that the PDP-11,  like
; so many other machines, divides incorectly; that is, the
; remainder has the same sign as the dividend.
;
; Inputs:
;	r0=the number
;	r1=pointer to buffer
;
; Outputs:
;	r1=updated
;-
 
$itoc:	mov	r2,-(sp)	;Get work registers
	mov	r3,-(sp)
	mov	r0,r2		;Move number to be converted
	bpl	10$		;Br if >= 0
	movb	#'-,(r1)+	;If -ve, put out - sign
	br	20$
10$:	neg	r2		;If positive, make negative
20$:	call	30$		;Perform recursive conversion
	mov	(sp)+,r3	;Restore registers
	mov	(sp)+,r2
	return			;Return to caller
 
30$:	ashc	#-16.,r2	;Set up for divide
	div	#10.,r2		;Compute a digit
	beq	40$		;Quotient=0 => done
	mov	r3,-(sp)	;Save remainder, then
	call	30$		;Recurse on quotient
	mov	(sp)+,r3	;Get remainder back
40$:	neg	r3		;Character is
	add	#'0,r3		;'0'-remainder
	movb	r3,(r1)+	;To buffer
	return			;Done
 
	.end
