	.define mlu4
	.define	dvu4
.sect .text
.sect .rom
.sect .data
.sect .bss
.sect .text
!----------------------------------------------------------------------------
!		SIGNED INTEGER ARITHMETIC
!------------------------------------------------------------------------------

adi_l:		adroff		;	move.w	(a5),d0	;	bra 1f
adi_z:		mov	(sp)+,d0
1:		sub.w	wrd,d0	;	beq	adi_1W
		sub.w	wrd,d0	;	beq	adi_2W
		bra	e_oddz
adi_1W:
#if test
		comp	und,(sp) ;	beq	6f
		comp	und,word(sp);	bne	7f
6:		bsr	e_iund
#endif
7:		mov	(sp)+,d0 ;	ad	d0,(sp)
#if test
		bvs	9f
#endif
		jmp	(a4)
adi_2W:
#ifdef lword
		bsr	no8bar	;	add.l	#8,sp
		jmp	(a4)
#else
		move.l	(sp)+,d0 ;	add.l	d0,(sp)
#endif
#if test
		bvs	9f
#endif
		jmp	(a4)

!--------------------------------------------------------------------------

sbi_z:		mov	(sp)+,d0 ;	bra	1f
sbi_l:		adroff		;	move.w	(a5),d0
1:		sub.w	wrd,d0	;	beq	sbi_1W
		sub.w	wrd,d0	;	beq	sbi_2W
		bra	e_oddz
sbi_1W:
#if test 
		comp	und,(sp) ;	beq	6f
		comp	und,word(sp) ; bne	7f
6:		bsr	e_iund
#endif
7:		mov	(sp)+,d0 ;	subt	d0,(sp)
#if test
		bvs	9f
#endif
		jmp	(a4)
sbi_2W:
#ifdef lword
		add.l	#8,sp	;	bsr	no8bar
		jmp	(a4)
#else
		move.l	(sp)+,d0 ;	sub.l	d0,(sp)
#endif
#if test
		bvs	9f
#endif
		jmp	(a4)
9:		bsr	e_iovfl ;	jmp	(a4)

!----------------------------------------------------------------------------

mli_z:		mov	(sp)+,d0 ;	bra	0f
mli_l:		adroff		;	move.w	(a5),d0
0:		sub.w	wrd,d0	;	beq	mli_1W
		sub.w	wrd,d0	;	beq	mli_2W
		bra	e_oddz
mli_1W:		mov	(sp)+,d0
#if test
		comp	und,d0 ;	beq	1f
		comp	und,(sp) ;	bne	2f
1:		bsr	e_iund
#endif
2:
#ifdef lword
		move.l	(sp)+,d1 ;	bra	4f
#else
		muls	(sp),d0 ;	move.w	d0,(sp)
#if test
		bpl	3f	;	not.l	d0
3:		swap	d0	;	tst.w	d0	;	bne 9b
#endif
		jmp	(a4)
#endif
mli_2W:
#ifdef lword
		bsr	no8bar	;	add.l	#4,sp
		move.l	(sp)+,d0 ;	add.l	#4,sp
		move.l	(sp)+,d1
#else
		move.l	(sp)+,d0 ;	move.l	(sp)+,d1
#endif
4:		move.l	#0,d5	;	tst.l	d0	;	bpl 5f
		neg.l	d0	;	not.w	d5
5:		tst.l	d1	;	bpl	6f
		neg.l	d1	;	not.w	d5
6:		bsr	mlu4
#if test 
		tst.l	d4	;	bne	7f
		tst.l	d0	;	bpl	8f
7:		bsr	e_iovfl
#endif
8:		tst.w	d5	;	beq	0f
		neg.l	d0
0:		move.l	d0,-(sp) 
!next 4 lines only in case 8 byte arithmetic
!#ifdef lword
!		bmi	1f	;	clr.l	-(sp)	;	bra 2f
!1:		move.l	#-1,-(sp)
!#endif
2:		jmp	(a4)

!subroutine for unsigned 4byte multiplication . Expects multiplier in d0 and
! multiplicant in d1 . Returns 4 byte result in d0 . If d4=0 overflow did 
! not occur on the multiplication , else it did .

	.sect .text

mlu4:		move.l	d1,d3	;	move.l	d0,d2
		swap	d2	;	swap	d3
#if test
		move.l	d3,d4	;	mulu	d2,d4
#endif
		mulu	d0,d3	;	swap	d3
		mulu	d1,d2	;	swap	d2
#if test
		or.w	d3,d4	;	or.w	d2,d4
#endif
		clr.w	d3	;	clr.w	d2
		mulu	d1,d0	;	add.l	d3,d0
#if test
		bvc	1f	;	bset	#0,d4
#endif
1:		add.l	d2,d0
#if test
		bvc	2f	;	bset	#0,d4
#endif
2:		rts

!---------------------------------------------------------------------------

dvi_z:		mov	(sp)+,d0 ;	bra	0f
dvi_l:		adroff		;	move.w	(a5),d0
0:		sub.w	wrd,d0	;	beq	dvi_1W
		sub.w	wrd,d0	;	beq	dvi_2W
		bra	e_oddz
dvi_1W:
#ifdef lword
		bsr	dvi4	;	move.l	d1,-(sp)
#else
		bsr	dvi2	;	move.w	d1,-(sp)
#endif
		jmp	(a4)
dvi_2W:
#ifdef lword
		bsr	no8bar	;	tst.l	(sp)+
		move.l	(sp)+,(sp) ;	bsr	dvi4
		move.l	d1,-(sp) ;	clr.l	-(sp)
#else
		bsr	dvi4	;	move.l	d1,-(sp)
#endif
		jmp	(a4)

rmi_z:		mov	(sp)+,d0 ;	bra	1f
rmi_l:		adroff		;	move.w	(a5),d0
1:		sub.l	wrd,d0	;	beq	rmi_1W
		sub.l	wrd,d0	;	beq	rmi_2W
		bra	e_oddz
rmi_1W:
#ifdef lword
		bsr	dvi4	;	move.l	d3,-(sp)
#else
		bsr	dvi2	;	swap	d1
		move.w	d1,-(sp)
#endif
		jmp	(a4)
rmi_2W:
#ifdef lword
		bsr	no8bar	;	tst.l	(sp)+
		move.l	(sp)+,(sp) ;	bsr	dvi4
		move.l	d3,-(sp) ;	clr.l	-(sp)
#else
		bsr	dvi4	;	move.l	d3,-(sp)
#endif
		jmp	(a4)

! 2byte division . In d1: quotient=low word ; remainder=high word
#ifndef lword
dvi2:		move.l	(sp)+,d2
		move.w	(sp)+,d0				!divisor
		move.w	(sp)+,d1 ;	ext.l	d1		!dividend
#if test
		cmp.w	und,d1	;	bne	1f
		bsr	e_iund
1:		cmp.w	und,d0	;	bne	2f
		bsr	e_iund
2:		tst.w	d0	;	bne	3f
		bsr	e_idivz	;	move.l	und,d1	;	bra 4f
3:
#endif
		divs	d0,d1
4:		move.l	d2,-(sp) ;	rts
#endif

! long signed division . quotient in d1 , remainder in d3
dvi4:		move.l	(sp)+,d5
		move.l	(sp)+,d0		!divisor
		move.l	(sp)+,d1		!dividend
#ifdef lword
		cmp.l	und,d0	;	beq	0f
		cmp.l	und,d1	;	bne	1f
0:		bsr	e_iund
1:
#endif
		move.l	#0,d4			!sign in d4
		tst.l	d0	;	bpl	1f
		neg.l	d0	;	not.w	d4
1:		tst.l	d1	;	bpl	2f
		neg.l	d1
		not.w	d4	;	swap	d4
		not.w	d4	;	swap	d4
2:		bsr	dvu4
		tst.w	d4	;	beq	3f
		neg.l	d1			!quotient
3:		tst.l	d4	;	bpl	4f
		neg.l	d3				!remainder
4:		move.l	d5,-(sp) ;	rts

!Expects d0 divisor , d1 dividend. Gives d1 quotient ,d3 remainder

	.sect .text
dvu4:
#if test
		tst.l	d0	;	bne	1f
		bsr	e_idivz
1:
#endif
		move.l	#0,d3	;	move.l	#32,d2
3:		lsl.l	#1,d1	;	roxl.l	#1,d3
		cmp.l	d0,d3	;	blt	4f
		sub.l	d0,d3	;	add.l	#1,d1
4:		sub.w	#1,d2	;	bgt	3b
		rts

!----------------------------------------------------------------------------

ngi_z:		mov	(sp)+,d0 ;	bra	0f
ngi_l:		adroff		;	move.w	(a5),d0
0:		sub.l	wrd,d0	;	bne	2f
#if test
		comp	und,(sp) ;	bne	1f
		bsr	e_iund
1:
#endif
		nega	(sp)	;	jmp	(a4)
2:		cmp.l	wrd,d0	;	beq	3f
		bra	e_oddz
3:
#ifdef lword
		bsr	no8bar	;	not.l	(sp)
		neg.l	4(sp)
#else
		neg.l	(sp)
#endif
#if test
		bvc	4f	;	bsr	e_iovfl
4:
#endif
		jmp	(a4)

!--------------------------------------------------------------------------

sli_z:		mov	(sp)+,d0 ;	bra	0f
sli_l:		adroff		;	move.w	(a5),d0
0:		sub.w	wrd,d0	;	beq	sli_1W
		sub.w	wrd,d0	;	beq	sli2
		bra	e_oddz
sli_1W:		mov	(sp)+,d0 	!d0 contains the shift count
		bmi	5f
9:		mov	(sp)+,d1		!integer to shift 
#if test
		comp	und,d0	;	bne	1f
		bsr	e_iund
1:
#endif
		asle	d0,d1			! ASLE
#if test
		bvc	2f	;	bsr	e_iovfl
2:
#endif
		mov	d1,-(sp) ;	jmp	(a4)
sli2:
#ifdef lword
		bsr	no8bar	;	move.l	(sp)+,d1
		move.l	(sp)+,d2 ;	move.l	(sp)+,d0
3:		asl.l	#1,d0	;	roxl.l	#1,d2
		sub.l	#1,d1	;	bgt	3b
		move.l	d0,-(sp) ;	move.l	d2,-(sp)
#else
		move.w	(sp)+,d0 
		bmi	6f
8:		move.l	(sp),d1
		asl.l	d0,d1
#if test
		bvc	4f	;	bsr	e_iovfl
4:
#endif
		move.l	d1,(sp)
#endif
		jmp	(a4)
5:		nega	d0	;	bra	8f
#ifndef	lword
6:		neg.w	d0	;	bra	9f
#endif

!------------------------------------------------------------------------------
7:		nega	d0	;	bra	9b
#ifndef	lword
6:		neg.w	d0	;	bra	8b
#endif

sri_z:		mov	(sp)+,d0 ;	bra	0f
sri_l:		adroff		;	move.w	(a5),d0
0:		sub.w	wrd,d0	;	bne	sri2
		mov	(sp)+,d0 
		bmi	7b
8:		mov	(sp)+,d1
#if test
		comp	und,d0	;	bne	1f
		bsr	e_iund
1:
#endif
		asri	d0,d1
#if test
		bvc	2f	;	bsr	e_iovfl
2:
#endif
		mov	d1,-(sp) ;	jmp	(a4)
sri2:		sub.w	wrd,d0	;	beq	3f
		bra	e_oddz
3:
#ifdef lword
		bsr	no8bar	;	move.l	(sp)+,d1
		move.l	(sp)+,d2 ;	move.l	(sp),d0
		sub.l	#1,d1
4:		asr.l	#1,d2	;	roxr.l	#1,d0	;	dbra d1,4b
		move.l	d0,(sp) ;	move.l	d2,-(sp)
#else
		move.w	(sp)+,d0 
		bmi	6b
9:		move.l	(sp),d1
		asr.l	d0,d1
#if test
		bvc	5f	;	bsr	e_iovfl
5:
#endif
		move.l	d1,(sp)
#endif
		jmp	(a4)
