; (C) 1993 Peter Thompson
; 32 bit integer to general base conversion routines.

	INCLUDE	"intdefs.i"

	IFD	ASM_ENTRY
	INCLUDE	"intconv32.i"
	ENDIF

	IFD	STKARGS
	PUBLIC	_utostr32
_utostr32:			;C stkargs entry
	move.l	4(sp),a0	;C char *
	move.l	8(sp),d0	;C uint32
	move.l	12(sp),d1	;C int
	bra	u2str32
	ENDIF

	IFD	STKARGS
	PUBLIC	_itostr32
_itostr32:			;C stkargs entry
	move.l	4(sp),a0	;C char *
	move.l	8(sp),d0	;C int32
	move.l	12(sp),d1	;C int
	ENDIF

	IFD	REGARGS
	PUBLIC	@itostr32
@itostr32:			;C regargs entry
	ENDIF	

i2str32:	; converts a signed int32 to a string in base Base.
	tst.l	d0
	bpl	us32	; treat positive numbers as unsigned
	PutCh	#'-'	; for negative numbers : put a minus sign
	neg.l	d0	; followed by the absolute value of the number

	IFD	REGARGS
	PUBLIC	@utostr32
@utostr32:			;C regargs entry
	ENDIF

u2str32:	; u2str32 is identical to us32.

SWAPBUF	EQU	-(32+4)		; generate lsb first

us32:		; Converts an unsigned int32 to a string in base Base.
	link	a5,#SWAPBUF
	push	Digit/Base
	move.l	d1,Base
	lea	SWAPBUF(a5),RevStr
	clr.b	(RevStr)+
	subq.l	#2,d1
	bcs	Berr32		;V
	cmp.l	#36+1,Base
	bcs	us32a		;V
Berr32:	move.l	#10,Base	; Bad choice?
us32a:	move.l	d0,d1		; 4 num[31..16] : num[15..0]
	clr.w	d0		; zero previous remainder
	swap	d0		; 4 previous remainder : num[31..16]
	divu	Base,d0		; 140 temp remainder : new quotient high 16
	move.w	d0,Digit	; 4 save quotient high 16
	move.w	d1,d0		; 4 temp remainder : num[15..0]
	divu	Base,d0		; 140 new remainder : new quotient low 16
	swap	Digit		; 4 prepare to assemble new quotient
	move.w	d0,Digit	; 4 new quotient in Digit
	clr.w	d0		; 4 new remainder in upper half of d0
	swap	d0		; 4 now in lower half
	exg	d0,Digit	; 6 now remainder in Digit and quotient in d0
	add.b	#'0',Digit
	cmp.b	#'9'+1,Digit
	bcs	PutIt
	add.b	#'A'-'0'-10,Digit
PutIt:	move.b	Digit,(RevStr)+
	tst.l	d0
	bne	us32a
strrev:	PutCh	-(RevStr)
	bne	strrev
us32x:	pop	Digit/Base
	move.l	String,d0
	unlk	a5
	rts

	END

