
SAD	MACRO
	movem.l	d0-d7/a0-a6,-(sp)
	ENDM

GAD	MACRO
	movem.l	(sp)+,d0-d7/a0-a6
	ENDM
	
SAD2	MACRO
	movem.l	d0-d5,-(sp)
	ENDM

GAD2	MACRO
	movem.l	(sp)+,d0-d5
	ENDM

SAD3	MACRO
	movem.l	d0-d1,-(sp)
	ENDM

GAD3	MACRO
	movem.l	(sp)+,d0-d1
	ENDM

SAD4	MACRO
	movem.l	d0-d6,-(sp)
	ENDM

GAD4	MACRO
	movem.l	(sp)+,d0-d6
	ENDM

SAD5	MACRO
	movem.l	d0-d7,-(sp)
	ENDM

GAD5	MACRO
	movem.l	(sp)+,d0-d7
	ENDM

	NOLIST
	INCLUDE	'exec/types.i'
	INCLUDE	'exec/libraries.i'
	INCLUDE	'exec/lists.i'
	INCLUDE	'exec/alerts.i'
	INCLUDE	'exec/initializers.i'
	INCLUDE	'exec/resident.i'
	INCLUDE	'exec/asmsupp.i'
	INCLUDE	'libraries/ias.i'
	
_AbsExecBase	= $4
_LVOOpenLibrary	= -552
_LVOCloseLibrary	= -414
_LVOAlert	= -108
_LVOFreeMem	= -210
_LVORemove	= -252

start
	moveq	#-1,d0
	rts
	
MYPRI	= 0

initDDescrip
	dc.w	RTC_MATCHWORD
	dc.l	initDDescrip
	dc.l	EndCode
	dc.b	RTF_AUTOINIT
	dc.b	VERSION
	dc.b	NT_LIBRARY
	dc.b	MYPRI
	dc.l	iasName
	dc.l	idstring
	dc.l	init
	
iasName	IASNAME
	even
VERSION	= 33
REVISION	= 1

idstring	dc.b 'IASlibrary 33.1 ( (c)Craig Eales 20 Sep 1991)',13,10,0

	ds.w	0
	
init
	dc.l	IasBase_SIZEOF
	dc.l	funcTable
	dc.l	dataTable
	dc.l	initRoutine

funcTable
	dc.l	Open
	dc.l	Close
	dc.l	Expunge
	dc.l	Null
	
	dc.l	INTmulu
	dc.l	INTdivu
	dc.l	IASdivs
	dc.l	IASabs
	dc.l	IASneg
	dc.l	IAScif
	dc.l	IAScfi
	dc.l	IASsub
	dc.l	IASadd
	dc.l	IASmuls
	dc.l	IASdivi
	dc.l	IAScfa
	dc.l	INTcia
	
	dc.l	-1
	
dataTable
	INITBYTE	LN_TYPE,NT_LIBRARY
	INITLONG	LN_NAME,iasName
	INITBYTE	LIB_FLAGS,LIBF_SUMUSED!LIBF_CHANGED
	INITWORD	LIB_VERSION,VERSION
	INITWORD	LIB_REVISION,REVISION
	INITLONG	LIB_IDSTRING,idstring
	dc.l	0

initRoutine
	move.l	a5,-(sp)
	move.l	d0,a5
	move.l	a6,iasb_SysLib(a5)
	move.l	a0,iasb_SegList(a5)
	move.l	a5,d0
	move.l	(sp)+,a5
	rts

Open
	addq.w	#1,LIB_OPENCNT(a6)
	bclr	#LIBB_DELEXP,iasb_Flags(a6)
	move.l	a6,d0
	rts

Close
	CLEAR	d0
	subq.w	#1,LIB_OPENCNT(a6)
	bne.s	.templab
	btst	#LIBB_DELEXP,iasb_Flags(a6)
	beq.s	.templab
	bsr	Expunge
.templab
	rts

Expunge
	movem.l	d2/a5/a6,-(sp)
	move.l	a6,a5
	move.l	iasb_SysLib(a5),a6
	tst.w	LIB_OPENCNT(a5)
	beq.s	.templab
	bset	#LIBB_DELEXP,iasb_Flags(a5)
	CLEAR	d0
	bra.s	Expunge_End
.templab
	move.l	iasb_SegList(a5),d2
	move.l	a5,a1
	CALLSYS	Remove
	
	CLEAR	d0
	move.l	a5,a1
	move.w	LIB_NEGSIZE(a5),d0
	sub.l	d0,a1
	add.w	LIB_POSSIZE(a5),d0
	CALLSYS	FreeMem
	move.l	d2,d0
Expunge_End
	movem.l	(sp)+,d2/a5/a6
	rts
	
Null
	CLEAR	d0
	rts
	
	
***** 32 bit multiply  ******
;times d0*d1 to d2
INTmulu
	movem.l	d0-d4,-(a7)
	
	clr.l	d4
	
	move.l	d0,d2
	move.l	d1,d3
	
	swap	d2
	and.l	#$ffff,d2
	
	and.l	#$ffff,d3
	mulu	d2,d3
	
	and.l	#$ffff,d3
	swap	d3
	
	add.l	d3,d4
	
	move.l	d0,d2
	move.l	d1,d3
	
	swap	d3
	and.l	#$ffff,d3
	
	and.l	#$ffff,d2
	mulu	d2,d3
	
	and.l	#$ffff,d3
	swap	d3
	
	add.l	d3,d4
	
	move.l	d0,d2
	move.l	d1,d3
	and.l	#$ffff,d2
	and.l	#$ffff,d3
	mulu	d2,d3
	add.l	d3,d4
	
	move.l	d4,result
	movem.l	(a7)+,d0-d4
	move.l	result,d2
	rts
	
***** ARP 32 bit divide *****
;32 bit divide d0/d1 to d2
INTdivu
	movem.l	d0-d3,-(a7)
	swap	d1
	tst.w	d1
	bne.s	lab5
	swap	d1
	move.w	d1,d3
	move.w	d0,d2
	clr.w	d0
	swap	d0
	divu	d3,d0
	move.l	d0,d1
	swap	d0
	move.w	d2,d1
	divu	d3,d1
	move.w	d1,d0
	clr.w	d1
	swap	d1
	move.l	d0,result
	movem.l	(a7)+,d0-d3
	move.l	result,d2
	rts
lab5
	swap	d1
	move.l	d1,d3
	move.l	d0,d1
	clr.w	d1
	swap	d1
	swap	d0
	clr.w	d0
	moveq	#$f,d2
lab7
	add.l	d0,d0
	addx.l	d1,d1
	cmp.l	d1,d3
	bhi.s	lab6
	sub.l	d3,d1
	addq.w	#1,d0
lab6
	dbf	d2,lab7
	move.l	d0,result
	movem.l	(a7)+,d0-d3
	move.l	result,d2
	rts

***** Full IAS divide ***
;divide d0 by d1 to d2 (all IAS)
IASdivs
	SAD5
	move.l	d0,d3
	move.l	d1,d4
	
	and.l	#$7ffffff,d0
	and.l	#$7ffffff,d1
	
	move.l	d0,d5
	move.l	d1,d6
	
	jsr	INTdivu		;now in d7 ab/cd=e
	move.l	d2,d7
	
	and.l	#$ffff,d7
	swap	d7
	
	SAD4
	move.l	d2,d0
	move.l	d6,d1
	jsr	INTmulu
	move.l	d2,result
	GAD4
	move.l	result,d6
	
	sub.l	d6,d5		;rem now in d5
	
	move.l	d0,d6
	move.l	#$80000000,d0
	jsr	INTdivu
	lsl.l	#1,d2
	
	SAD4
	move.l	d5,d0
	move.l	d2,d1
	jsr	INTmulu
	move.l	d2,result
	GAD4
	move.l	result,d2

	swap	d2
	and.l	#$ffff,d2
	
	add.l	d2,d7
	
	eor.l	d3,d4
	and.l	#$80000000,d4
	or.l	d4,d7
	
	move.l	d7,result
	GAD5
	move.l	result,d2
	rts
	
	


***** Absolute IAS ******
;find abs value of d0
IASabs
	and.l	#$7ffffff,d0
	move.l	d0,result
	rts

***** Negate IAS ********
;Make IAS in d0 neg
IASneg
	eor.l	#$80000000,d0
	move.l	d0,result
	rts
		
***** Integer To  IAS *****	
;convert integer in d0 to IAS
IAScif
	tst.l	d0
	bmi.s	.doneg
	
	and.l	#$ffff,d0
	swap	d0
	
	move.l	d0,result
	rts
.doneg
	neg.l	d0
	and.l	#$ffff,d0
	swap	d0
	
	or.l	#$80000000,d0
	move.l	d0,result
	rts

******* IAS to Integer ****
;convert IAS in d0 to integer
IAScfi
	SAD3
	move.l	d0,d1
	and.l	#$80000000,d1
	bne.s	.negbit
	
	swap	d0
	and.l	#$ffff,d0
	
	move.l	d0,result
	GAD3
	move.l	result,d0
	rts
.negbit
	and.l	#$7fffffff,d0
	
	swap	d0
	and.l	#$ffff,d0
	
	neg.l	d0
	move.l	d0,result
	GAD3
	move.l	result,d0
	rts
	
***** Fixed Point Sub *****
;Sub d1 from d0 and store in d2
IASsub
	SAD3
	eor.l	#$80000000,d1
	jsr	IASadd
	GAD3
	move.l	result,d2
	rts
		
***** Fixed Point Add *****
;add d0 to d1 and put in d2
IASadd
	SAD2
	move.l	d0,d2
	move.l	d1,d3
	move.l	d1,d4
	eor.l	d2,d4
	and.l	#$80000000,d4
	beq.s	.justaddem

	move.l	d0,d4
	and.l	#$80000000,d4
	bne.s	.swopem
.swoped
	and.l	#$7fffffff,d2
	and.l	#$7fffffff,d3
	sub.l	d3,d2,
	cmp.l	d3,d0
	blt.s	.makeneg
.madeneg
	move.l	d2,result
	GAD2
	move.l	result,d2
	rts

.swopem
	move.l	d0,d4
	move.l	d1,d0
	move.l	d4,d1
	move.l	d0,d2
	move.l	d1,d3
	bra.s	.swoped
.makeneg	
	neg.l	d2
	or.l	#$80000000,d2
	bra.s	.madeneg
	
.justaddem
	move.l	d1,d5
	and.l	#$7fffffff,d5
	and.l	#$7fffffff,d2
	add.l	d5,d2
	move.l	d0,d4
	and.l	#$80000000,d4
	or.l	d4,d2
	move.l	d2,result
	GAD2
	move.l	result,d2
	rts


**** Fixed Point Multiply ****
;Times d0 and d1 into d2
IASmuls
	SAD2
	move.l	d0,d2
	move.l	d1,d3
	
	and.l	#$7fffffff,d3
	and.l	#$7fffffff,d2
	
	swap	d3
	and.l	#$ffff,d3
	swap	d2
	and.l	#$ffff,d2
	
	mulu	d3,d2
	
	and.l	#$ffff,d2
	swap	d2
	
	move.l	d0,d3
	move.l	d1,d4
	and.l	#$7fffffff,d3
	and.l	#$ffff,d4
	
	swap	d3
	and.l	#$ffff,d3

	mulu	d3,d4
	add.l	d4,d2
	
	move.l	d0,d3
	move.l	d1,d4
	and.l	#$ffff,d3
	and.l	#$7fffffff,d4
	
	swap	d4
	and.l	#$ffff,d4
	
	mulu	d3,d4
	add.l	d4,d2

	move.l	d0,d3
	move.l	d1,d4
	and.l	#$ffff,d3
	and.l	#$ffff,d4
	mulu	d3,d4
	
	swap	d4
	and.l	#$ffff,d4
	
	add.l	d4,d2
	
	eor.l	d0,d1
	and.l	#$80000000,d1
	beq.s	.end
	or.l	#$80000000,d2
.end
	move.l	d2,result
	GAD2
	move.l	result,d2
	rts

**** Divide IAS by integer **
;Divides IAS in d0 by integer in d1 to IAS in d2
IASdivi
	SAD2
	move.l	d0,d2
	move.l	d1,d3
	clr.l	d5
	clr.l	d4
	and.l	#$7fffffff,d2
	tst.l	d3
	bpl.s	.isplus
	neg.l	d3
.isplus
	
	
	swap	d2
	and.l	#$ffff,d2
	
	divu	d3,d2
	swap	d2
	move.w	d2,d5
	swap	d2
	
	and.l	#$ffff,d2
	swap	d2
	
	move.l	#$00010000,d4
	divu	d3,d4
	and.l	#$ffff,d4
	mulu	d4,d5
	add.l	d5,d2
	
	move.l	d0,d4
	and.l	#$ffff,d4
	move.l	d1,d5
	divu	d5,d4
	and.l	#$ffff,d4
	add.l	d4,d2
	
	move.l	d0,d3
	and.l	#$80000000,d3
	move.l	d1,d4
	tst.l	d4
	bmi.s	.divbyneg
	
	tst.l	d3
	beq.s	.doneall
	or.l	#$80000000,d2
	bra.s	.doneall
.divbyneg
	tst.l	d3
	bne.s	.doneall
	or.l	#$80000000,d2
.doneall	
	move.l	d2,result
	GAD2
	move.l	result,d2
	rts
	
**** Convert IAS to ASCII ***
;Converts IAS in d0 to ASCII at a0 (12 bytes)
IAScfa
	SAD
	move.l	d0,d1
	and.l	#$80000000,d1
	beq.s	.doplus
	move.b	#'-',(a0)+
	bra.s	.donsign
.doplus
	move.b	#'+',(a0)+
.donsign
	and.l	#$7fffffff,d0
	move.l	d0,d2
	move.l	a0,a1
	
	swap	d0
	and.l	#$ffff,d0
	
	clr.l	d1
	move.l	#buffer1,a0
	jsr	convert
	add.l	#5,a0
	move.b	(a0)+,(a1)+
	move.b	(a0)+,(a1)+
	move.b	(a0)+,(a1)+
	move.b	(a0)+,(a1)+
	move.b	(a0)+,(a1)+
	move.b	#'.',(a1)+
	
	move.l	d2,d0
	and.l	#$ffff,d0
	move.l	#15259,d1
	mulu	d1,d0
	clr.l	d1
	move.l	#buffer1,a0
	jsr	convert
	add.l	#1,a0
	move.b	(a0)+,(a1)+
	move.b	(a0)+,(a1)+
	move.b	(a0)+,(a1)+
	move.b	(a0)+,(a1)+
	move.b	#0,(a1)+
	GAD
	rts
	
**** Convert Integer to ASCII ***
; INT d0 to a0
INTcia	
	SAD
	move.l	#$1,d1
	jsr	convert
	GAD
	rts
	
**** Convert d0 to ASCII ****
;d0 source
;d1 Flags - bit 0 signed - bit 1 strip
;a0 dest
convert
	SAD
	and.l	#$1,d1
	beq	.donesign
	tst.l	d0
	bmi.s	.dominus
	move.b	#'+',(a0)+
.donesign	
	lea	cols,a1
	move.w	#9,d3
.digits	
	move.l	(a1)+,d1
	bsr.s	.dodigit
	dbra	d3,.digits
	GAD
	SAD
	move.l	d1,d2
	and.l	#$2,d1
	beq.s	.removed
	and.l	#$1,d2
	beq.s	.strip
	addq.l	#1,a0
.strip
	cmp.b	#'0',(a0)
	bne.s	.removed
	bra.s	.remove
.removed
	GAD
	rts

.dodigit
	clr.l	d2
.loop
	addq.l	#1,d2
	sub.l	d1,d0
	bpl.s	.loop
	add.l	d1,d0
	subq.l	#1,d2
	add.b	#'0',d2
	move.b	d2,(a0)+
	rts
.dominus
	move.b	#'-',(a0)+
	not.l	d0
	add.l	#1,d0
	bra.s	.donesign
.remove
	SAD
	move.l	#9,d1
.nexts
	move.b	1(a0),(a0)+
	dbra	d1,.nexts
	GAD
	bra.s	.strip
	
cols	dc.l	1000000000,100000000,10000000
	dc.l	1000000,100000,10000,1000,100,10,1
	
	
**** FLAGS ETC *****
result	dc.l	0
buffer1	dc.l	0,0,0

EndCode:	END
