index.asm
; Copyright (C) 1984 by Manx Software Systems, Inc.
; :ts=8
;
	public _index
_index
	move.l	4(sp),a0
	IF INT32
	move.l	8(sp),d0
	ELSE
	move.w	8(sp),d0
	ENDC
.1
	move.b	(a0)+,d1
	beq	.2
	cmp.b	d0,d1
	bne	.1
	move.l	a0,d0
	sub.l	#1,d0
	rts
.2
	move.l	#0,d0
	rts
;
ldiv.asm
; Copyright (C) 1984 by Manx Software Systems, Inc.
;:ts=8
;
	public	.divs
.divs:		;long divide	(primary = primary/secondary)
	move.l	d4,-(sp)
	clr.l	d4			;mark result as positive
	tst.l	d0
	bpl.s	prim_ok
	neg.l	d0
	add.w	#1,d4			;mark as negative
prim_ok:
	tst.l	d1
	bpl.s	sec_ok
	neg.l	d1
	eor.w	#1,d4			;flip sign of result
sec_ok:
	jsr	comdivide
chksign:
	tst.w	d4
	beq.s	posres
	neg.l	d0
posres:
	move.l	(sp)+,d4
	rts
;
	public	.mods
.mods:		;long remainder	(primary = primary%secondary)
	move.l	d4,-(sp)
	clr.l	d4			;mark result as positive
	tst.l	d0
	bpl.s	rprim_ok
	neg.l	d0
	add.w	#1,d4			;mark as negative
rprim_ok:
	tst.l	d1
	bpl.s	rsec_ok
	neg.l	d1
	eor.w	#1,d4			;flip sign of result
rsec_ok:
	jsr	comdivide
	move.l	d1,d0
	jmp	chksign
;
	public	.modu
.modu:		;unsigned long remainder	(primary = primary%secondary)
	jsr	comdivide
	move.l	d1,d0
	rts
;
	public	.divu
.divu:		;unsigned long divide	(primary = primary/secondary)
;	fall thru into common divide routine
;
; divide (dx,ax) by (bx,cx):
;	quotient in (dx,ax)
;	remainder in (bx,cx)
comdivide:
	movem.l	d2/d3,-(sp)
	swap	d1		;check high word
	tst.w	d1		;check for easy case
	bne.s	hardldv
	swap	d1		;get low word back
	clr.w	d3
	divu	d1,d0
	bvc.s	format
	move.w	d0,d2
	clr.w	d0
	swap	d0
	divu	d1,d0
	move.w	d0,d3
	move.w	d2,d0
	divu	d1,d0
format:
	move.l	d0,d1
	swap	d0
	move.w	d3,d0
	swap	d0
	clr.w	d1
	swap	d1
	movem.l	(sp)+,d2/d3
	rts
hardldv:
	swap	d1
	clr.l	d2		;clear out top half of dividend
	move.l	#31,d3		;set up loop count
hardloop:
	asl.l	#1,d0
	roxl.l	#1,d2
	sub.l	d1,d2		;subtract divisor till negative
	bmi.s	zerobit
onebit:
	add.l	#1,d0		;set bit in quotient
	dbra	d3,hardloop
	bra.s	hard_done
zeroloop:
	asl.l	#1,d0
	roxl.l	#1,d2		;shift dividend left one bit
	add.l	d1,d2		;add divisor till positive
	bpl.s	onebit
zerobit:
	dbra	d3,zeroloop
	add.l	d1,d2		;add divisor in one more time to fix remainder
hard_done:
	move.l	d2,d1		;copy remainder
	movem.l	(sp)+,d2/d3
	rts
;
lmul.asm
; Copyright (C) 1984 by Manx Software Systems, Inc.
;:ts=8
;
	public	.mulu
.mulu:		;unsigned long multiply	(primary = primary*secondary)
;
	movem.l	d2/d3,-(sp)
	move.w	d1,d2
	mulu	d0,d2		;d0.l * d1.l
	move.l	d1,d3
	swap	d3
	mulu	d0,d3		;d0.l * d1.h
	swap	d3
	clr.w	d3
	add.l	d3,d2
	swap	d0
	mulu	d1,d0		;d0.h * d1.l
	swap	d0
	clr.w	d0
	add.l	d2,d0
	movem.l	(sp)+,d2/d3
	rts
;
makefile

.c.r:
	c68 +b $(CFLAGS) $*.c

.asm.r:
	as68 $*.asm

.asm.r32:
	as68 -EINT32 -o $@ $*.asm

ASM=index.r ldiv.r lmul.r movmem.r peek.r\
	rindex.r setjmp.r setmem.r strcat.r strcmp.r\
	strcpy.r strlen.r strncpy.r swapmem.r toupper.r
ASM32=index.r32 movmem.r32 peek.r32 rindex.r32 setjmp.r32\
	setmem.r32 strcat.r32 strcmp.r32 strncpy.r32 swapmem.r32\
	toupper.r32

all:	$(ASM) $(ASM32)
	echo All done!!

movmem.asm
; Copyright (C) 1984,85,86 by Manx Software Systems, Inc.
; :ts=8
;
	public _movmem
_movmem
	movem.l	4(sp),a0/a1
	move.l	#0,d0
	IF INT32
	move.l	12(sp),d0
	ELSE
	move.w	12(sp),d0
	ENDC
	cmp.l	a0,a1
	bne	.1
	rts
.1
	bls	move_forward
	add.w	d0,a0
	add.w	d0,a1
	bra	.3
.2
	move.b	-(a0),-(a1)
.3
	dbra	d0,.2
	rts
;
.4
	move.b	(a0)+,(a1)+
move_forward
	dbra	d0,.4
	rts
;
peek.asm
; Copyright (C) 1984 by Manx Software Systems, Inc.
; :ts=8
;
	public _peekl
_peekl
	move.l	4(sp),a0
	move.l	#0,d0
	move.w	#3,d1
.1
	lsl.l	#8,d0
	move.b	(a0)+,d0
	dbra	d1,.1
	rts
;
	public _peekw
_peekw
	move.l	4(sp),a0
	move.l	#0,d0
	move.b	(a0)+,d0
	lsl.w	#8,d0
	move.b	(a0),d0
	rts
;
	public _peek
	public _peekb
_peekb
_peek
	move.l	4(sp),a0
	move.l	#0,d0
	move.b	(a0),d0
	rts
;
	public _pokel
_pokel
	move.l	4(sp),a0
	lea	8(sp),a1
	move.w	#3,d1
.2
	move.b	(a1)+,(a0)+
	dbra	d1,.2
	rts
;
	public _pokew
_pokew
	move.l	4(sp),a0
	IF INT32
	move.b	10(sp),(a0)+
	move.b	11(sp),(a0)+
	ELSE
	move.b	8(sp),(a0)+
	move.b	9(sp),(a0)+
	ENDC
	rts
;
	public _pokeb
	public _poke
_pokeb
_poke
	move.l	4(sp),a0
	IF INT32
	move.b	11(sp),(a0)
	ELSE
	move.b	9(sp),(a0)
	ENDC
	rts
;
rindex.asm
; Copyright (C) 1984 by Manx Software Systems, Inc.
; :ts=8
;
	public _rindex
_rindex
	move.l	4(sp),a0
	move.l	a0,a1
.1
	tst.b	(a0)+
	bne	.1
	sub.l	#1,a0
	IF INT32
	move.b	11(sp),d0
	ELSE
	move.b	9(sp),d0
	ENDC
.2
	cmp.l	a0,a1
	beq	.3
	cmp.b	-(a0),d0
	bne	.2
	move.l	a0,d0
	rts
.3
	move.l	#0,d0
	rts
;
setjmp.asm
; Copyright (C) 1984 by Manx Software Systems, Inc.
; :ts=8
;
	public _setjmp
_setjmp
	move.l	(sp)+,a1
	move.l	(sp),a0
	movem.l	d4-d7/a1-a7,(a0)
	move.l	#0,d0
	jmp	(a1)
;
	public	_longjmp
_longjmp
	move.l	4(sp),a0
	IF INT32
	move.l	8(sp),d0
	ELSE
	move.w	8(sp),d0
	ENDC
	bne	.1
	move.l	#1,d0
.1
	movem.l	(a0),d4-d7/a1-a7
	jmp	(a1)
;
setmem.asm
; Copyright (C) 1984 by Manx Software Systems, Inc.
; :ts=8
;
	public _setmem
_setmem
	move.l	4(sp),a0
	IF INT32
	movem.l	8(sp),d0/d1
	ELSE
	movem.w	8(sp),d0/d1
	ENDC
	bra	.2
.1
	move.b	d1,(a0)+
.2
	dbra	d0,.1
	rts
;
strcat.asm
; Copyright (C) 1984 by Manx Software Systems, Inc.
; :ts=8
;
	public _strcat,_strncat
_strcat
	move.w	#$7fff,d0
	bra	catcommon

_strncat
	IF INT32
	move.w	14(sp),d0
	ELSE
	move.w	12(sp),d0
	ENDC
;
catcommon
	move.l	4(sp),a0
.1
	tst.b	(a0)+
	bne	.1
	sub.w	#1,a0
	move.l	8(sp),a1
	sub.w	#1,d0
.2
	move.b	(a1)+,(a0)+
	dbeq	d0,.2
	clr.b	-(a0)
	move.l	4(sp),d0
	rts
;
strcmp.asm
; Copyright (C) 1984 by Manx Software Systems, Inc.
; :ts=8
;
	public _strcmp,_strncmp
_strcmp
	move.w	#$7fff,d0
	bra.s	cmpcommon

_strncmp
	IF INT32
	move.w	14(sp),d0
	ELSE
	move.w	12(sp),d0
	ENDC
;
cmpcommon
	sub.w	#1,d0
	bmi	equal
	move.l	4(sp),a0
	move.l	8(sp),a1
cmploop
	cmp.b	(a1)+,(a0)+
	bne	notequal
	sub.w	#1,a0
	tst.b	(a0)+
	dbeq	d0,cmploop
equal
	move.l	#0,d0
	rts
;
notequal
	bls	less
	move.l	#1,d0
	rts
;
less
	move.l	#-1,d0
	rts
;
strcpy.asm
; Copyright (C) 1984 by Manx Software Systems, Inc.
; :ts=8
;
	public _strcpy
_strcpy
	move.l	4(sp),a0
	move.l	a0,d0
	move.l	8(sp),a1
.1
	move.b	(a1)+,(a0)+
	bne.s	.1
	rts
;
strlen.asm
; Copyright (C) 1984 by Manx Software Systems, Inc.
; :ts=8
;
	public _strlen
_strlen
	move.l	4(sp),a0
	move.l	a0,d0
.1
	tst.b	(a0)+
	bne.s	.1
	sub.l	d0,a0
	move.l	a0,d0
	sub.l	#1,d0
	rts
;
strncpy.asm
; Copyright (C) 1984 by Manx Software Systems, Inc.
; :ts=8
;
	public _strncpy
_strncpy
	movem.l	4(sp),a0/a1
	move.l	a0,d0
	IF INT32
	move.l	12(sp),d1
	ELSE
	move.w	12(sp),d1
	ENDC
	bra	.2
.1
	move.b	(a1)+,(a0)+
.2
	dbeq	d1,.1
	add.w	#1,d1
	bra	.4
.3
	clr.b	(a0)+
.4
	dbra	d1,.3
	rts
;
swapmem.asm
; Copyright (C) 1984 by Manx Software Systems, Inc.
; :ts=8
;
	public _swapmem
_swapmem
	movem.l	4(sp),a0/a1
	IF INT32
	move.l	12(sp),d0
	ELSE
	move.w	12(sp),d0
	ENDC
	bra	.2
.1
	move.b	(a0),d1
	move.b	(a1),(a0)+
	move.b	d1,(a1)+
.2
	dbra	d0,.1
	rts
;
toupper.asm
; Copyright (C) 1984 by Manx Software Systems, Inc.
;:ts=8
;
	public _toupper
;
_toupper
	move.l	#0,d0
	IF INT32
	move.b	7(sp),d0
	ELSE
	move.b	5(sp),d0
	ENDC
	cmp.b	#'a'-1,d0
	bls	tu_done
	cmp.b	#'z',d0
	bhi	tu_done
	sub.b	#'a'-'A',d0
tu_done:
	rts
;
;
	public _tolower
;
_tolower
	move.l	#0,d0
	IF INT32
	move.b	7(sp),d0
	ELSE
	move.b	5(sp),d0
	ENDC
	cmp.b	#'A'-1,d0
	bls	tl_done
	cmp.b	#'Z',d0
	bhi	tl_done
	add.b	#'a'-'A',d0
tl_done:
	rts
;
