 section Maincode,code_p
start:		move.l	a0,-(a7)
		bsr.w	clearbss
		move.l	4,a6
		lea	dosn,a1
		jsr	-408(a6)
		move.l	d0,dosb
		move.l	dosb,a6
		jsr	-60(a6)
		move.l	d0,outputhandle
		beq.w	errorexit
		move.l	(a7)+,a0
		bsr.w	findparms	
		tst.l	d7
		bne.w	errorexit

		bsr.w	getfiles
		tst.l	d7
		bne.w	errorexit

		move.l	outputhandle,d1
		move.l	#introtext1,d2
		move.l	#introtext1end-introtext1,d3
		jsr	-48(a6)
		move.l	outputhandle,d1
		move.l	filename1,d2
		moveq	#0,d3
		move.w	filenamesize1,d3
		jsr	-48(a6)
		move.l	outputhandle,d1
		move.l	#introtext2,d2
		move.l	#introtext2end-introtext2,d3
		jsr	-48(a6)
		move.l	outputhandle,d1
		move.l	filename2,d2
		moveq	#0,d3
		move.w	filenamesize2,d3
		jsr	-48(a6)
		move.l	outputhandle,d1
		move.l	#enter,d2
		move.l	#4,d3
		jsr	-48(a6)
		bsr.s	compare
		cmp.l	#1,d7
		beq.s	filedifferent
		cmp.l	#2,d7
		beq.s	filereadwriteerror
		move.l	outputhandle,d1
		move.l	#filescompare,d2
		move.l	#filescompareend-filescompare,d3
		jsr	-48(a6)
		bra.s	errorexit

filedifferent:	move.l	outputhandle,d1
		move.l	#filesnotcompare,d2
		move.l	#filesnotcompareend-filesnotcompare,d3
		jsr	-48(a6)
		bra.s	errorexit

filereadwriteerror:
		move.l	outputhandle,d1
		move.l	#fileserrorcompare,d2
		move.l	#fileserrorcompareend-fileserrorcompare,d3
		jsr	-48(a6)

errorexit:	moveq	#0,d0
eop:		rts

compare:	move.l	filename1,d1
		move.l	#1005,d2
		jsr	-30(a6)
		move.l	d0,filehandle1
		beq.w	fileopenerror1
		move.l	filename2,d1
		move.l	#1005,d2
		jsr	-30(a6)
		move.l	d0,filehandle2
		beq.w	fileopenerror1

readfiles:	move.l	totread1,d0
		move.l	totread2,d1
		cmp.l	filesize1,d0
		beq.w	endreadfiles
		cmp.l	filesize2,d1
		beq.w	endreadfiles
		move.l	filehandle1,d1
		move.l	#filebuffer1,d2
		move.l	#4096,d3
		jsr	-42(a6)
		move.l	d0,tempread1

		move.l	filehandle2,d1
		move.l	#filebuffer2,d2
		move.l	#4096,d3
		jsr	-42(a6)
		move.l	d0,tempread2

		cmp.l	#4096,tempread1
		bne.s	notreadblockfile1
		add.l	#4096,totread1	
		bra.s	fileread1ok

notreadblockfile1:
		move.l	tempread1,d0
		add.l	d0,totread1
		move.l	totread1,d0
		cmp.l	filesize1,d0
		beq.s	fileread1ok
		move.l	outputhandle,d1
		move.l	filename1,d2
		moveq	#0,d3
		move.w	filenamesize1,d3
		jsr	-48(a6)
		bra.w	closefilesonerror

endreadfiles:	move.l	filehandle1,d1
		jsr	-36(a6)
		move.l	filehandle2,d1
		jsr	-36(a6)
		moveq	#1,d7
		tst.b	equal
		beq.s	donoteuqal
		rts
donoteuqal:	moveq	#0,d7
		rts

				
fileread1ok:	cmp.l	#4096,tempread2
		bne.s	notreadblockfile2
		add.l	#4096,totread2
		bra.s	fileread2ok
notreadblockfile2:
		move.l	tempread2,d0
		add.l	d0,totread2
		move.l	totread2,d0
		cmp.l	filesize2,d0
		beq.s	fileread2ok
		move.l	outputhandle,d1
		move.l	filename2,d2
		moveq	#0,d3
		move.w	filenamesize2,d3
		jsr	-48(a6)
		bra.w	closefilesonerror

fileread2ok:	move.w	#4095,d6
		lea	filebuffer1,a0
		lea	filebuffer2,a1
comparefiles:	cmpm.b	(a0)+,(a1)+
		bne.s	notequal
		dbf	d6,comparefiles
		addq.w	#1,totalblockreads
		bra.w	readfiles

notequal:	move.b	#1,equal
		tst.b	hexdisp
		bne.w	enditnow
		lea	hextable,a3
		move.l	a0,d0
		sub.l	#filebuffer1,d0
		move.w	totalblockreads,d1
		mulu	#4096,d1
		add.l	d1,d0
		lea	diftextend-1,a2
		moveq	#7,d5
setnumloop:	move.b	d0,d1
		and.w	#$f,d1
		lsr.l	#4,d0
		move.b	(a3,d1.w),-(a2)
		dbf	d5,setnumloop
		tst.w	d6
		beq.w	enditnow
		subq.w	#1,d6
		movem.l	a0-a4,-(a7)
		move.l	outputhandle,d1
		move.l	#diftext,d2
		move.l	#diftextend-diftext,d3
		jsr	-48(a6)
		move.l	outputhandle,d1
		move.l	#enter,d2
		move.l	#2,d3
		jsr	-48(a6)
		add.w	#1,difshow
		cmp.w	#15,difshow
		bne.s	contcomparefiles
		move.l	outputhandle,d1
		move.l	#enter,d2
		move.l	#4,d3
		jsr	-48(a6)
		move.l	outputhandle,d1
		move.l	#contcomparetext,d2
		move.l	#contcomparetextend-contcomparetext,d3
		jsr	-48(a6)
		move.l	outputhandle,d1
		move.l	#tmpbuffer,d2
		move.l	#2,d3
		jsr	-42(a6)
		cmp.b	#"y",tmpbuffer
		beq.s	contcomparefiles2
		cmp.b	#"Y",tmpbuffer
		beq.s	contcomparefiles2
		movem.l	(a7)+,a0-a4
		bra.s	enditnow
contcomparefiles2:
		clr.w	difshow
		move.l	outputhandle,d1
		move.l	#clearscreen,d2
		move.l	#1,d3
		jsr	-48(a6)
contcomparefiles:
		movem.l	(a7)+,a0-a4
		bra.w	comparefiles

enditnow:	move.l	filehandle1,d1
		jsr	-36(a6)
		move.l	filehandle2,d1
		jsr	-36(a6)
		moveq	#1,d7
		rts

closefilesonerror:
		move.l	filehandle1,d1
		jsr	-36(a6)
		move.l	filehandle2,d1
		jsr	-36(a6)
		moveq	#2,d7
		rts

fileopenerror1:	move.l	outputhandle,d1
		move.l	#fileopenerror,d2
		move.l	#fileopenerrorend-fileopenerror,d3
		jsr	-48(a6)
		move.l	filehandle1,d1
		beq.s	noclosefile1
		jsr	-36(a6)
noclosefile1:	move.l	filehandle2,d1
		beq.s	noclosefile2
		jsr	-36(a6)
noclosefile2:	moveq	#2,d7
		rts


clearbss:	lea	startbss,a0
		lea	endbss,a1
clearbssloop:	clr.b	(a0)+
		cmp.l	a0,a1
		bne.s	clearbssloop
		rts

getfiles:	move.l	dosb,a6
		move.l	filename1,d1
		move.l	#-2,d2
		jsr	-84(a6)
		move.l	d0,filelock1
		beq.w	lockerror1
		move.l	filelock1,d1
		move.l	#fileinfo,d2
		jsr	-102(a6)
		tst.l	d0
		beq.w	lockerror1
		lea	fileinfo,a0
		move.l	124(a0),filesize1
		move.l	filelock1,d1
		jsr	-90(a6)
		clr.l	filelock1

		move.l	filename2,d1
		move.l	#-2,d2
		jsr	-84(a6)
		move.l	d0,filelock2
		beq.w	lockerror2
		move.l	filelock2,d1
		move.l	#fileinfo,d2
		jsr	-102(a6)
		tst.l	d0
		beq.w	lockerror2
		lea	fileinfo,a0
		move.l	124(a0),filesize2
		move.l	filelock2,d1
		jsr	-90(a6)
		clr.l	filelock2
		move.l	filesize1,d0
		cmp.l	filesize2,d0
		beq.s	filessamesize
		move.l	outputhandle,d1
		move.l	#difsizetext,d2
		move.l	#difsizetextend-difsizetext,d3
		jsr	-48(a6)
		move.l	outputhandle,d1
		move.l	#filebuffer1,d2
		move.l	#2,d3
		jsr	-42(a6)
		cmp.b	#"Y",filebuffer1
		beq.s	filessamesize
		cmp.b	#"y",filebuffer1
		beq.s	filessamesize
		moveq	#1,d7
		rts
filessamesize:	moveq	#0,d7
		rts

lockerror1:	tst.l	filelock1
		beq.s	nofileunlock1
		move.l	filelock1,d1
		jsr	-90(a6)
nofileunlock1:	move.l	outputhandle,d1
		move.l	#filenotfoundtext,d2
		move.l	#filenotfoundtextend-filenotfoundtext,d3
		jsr	-48(a6)
		move.l	outputhandle,d1
		move.l	filename1,d2
		moveq	#0,d3
		move.w	filenamesize1,d3
		jsr	-48(a6)
		move.l	outputhandle,d1
		move.l	#enter,d2
		move.w	#4,d3
		jsr	-48(a6)
		moveq	#1,d7
		rts

lockerror2:	tst.l	filelock2
		beq.s	nofileunlock2
		move.l	filelock2,d1
		jsr	-90(a6)
nofileunlock2:	move.l	outputhandle,d1
		move.l	#filenotfoundtext,d2
		move.l	#filenotfoundtextend-filenotfoundtext,d3
		jsr	-48(a6)
		move.l	outputhandle,d1
		move.l	filename2,d2
		moveq	#0,d3
		move.w	filenamesize2,d3
		jsr	-48(a6)
		move.l	outputhandle,d1
		move.l	#enter,d2
		move.w	#4,d3
		jsr	-48(a6)
		moveq	#1,d7
		moveq	#1,d7
		rts

findparms:
findfirstparm:	cmp.b	#" ",(a0)
		beq.s	notfoundfirstparm
		cmp.b	#9,(a0)
		bne.s	foundfirstparm
notfoundfirstparm:
		addq.w	#1,a0
		bra.s	findfirstparm

foundfirstparm:	cmp.b	#$a,(a0)
		beq.w	noparmsgiven
		cmp.b	#"?",(a0)
		beq.w	noparmsgiven
		cmp.b	#"-",(a0)
		beq.s	specialoption
		move.l	a0,filename1
		moveq	#0,d0
findparm2:	cmp.b	#" ",(a0)
		beq.s	foundendofparm1
		cmp.b	#9,(a0)
		beq.s	foundendofparm1
		cmp.b	#$a,(a0)
		beq.s	noparmsgiven
		addq.w	#1,d0
		addq.w	#1,a0
		bra.s	findparm2

specialoption:	cmp.b	#"H",1(a0)
		beq.s	sethexdisp
		cmp.b	#"h",1(a0)
		beq.s	sethexdisp
		addq.w	#1,a0
		bra.s	foundfirstparm
sethexdisp:	move.b	#1,hexdisp
		addq.w	#2,a0
		bra.s	findfirstparm

foundendofparm1:clr.b	(a0)
		addq.w	#1,a0
		move.w	d0,filenamesize1
findsecondparm:	cmp.b	#$a,(a0)
		beq.s	noparmsgiven
		cmp.b	#" ",(a0)
		beq.s	notsecondparm
		cmp.b	#9,(a0)
		bne.s	secondparm
notsecondparm:	addq.w	#1,a0
		bra.s	findsecondparm

secondparm:	move.l	a0,filename2
		moveq	#0,d0
seekparm2end:	cmp.b	#$a,(a0)
		beq.s	foundparm2end
		addq.w	#1,a0
		addq.w	#1,d0
		bra.s	seekparm2end
foundparm2end:	clr.b	(a0)
		move.w	d0,filenamesize2
		moveq	#0,d7
		rts

noparmsgiven:	move.l	outputhandle,d1
		move.l	#explaintext,d2
		move.l	#explaintextend-explaintext,d3
		jsr	-48(a6)
		moveq	#1,d7
		rts

 section Maindata1,data_p
dosn:		dc.b	"dos.library",0
explaintext:	dc.b	"Compare V1.0 by Price of T.F.A.",10,13,10,13
		dc.b	"Compares 2 files and shows if identical or not.",10,13
		dc.b	10,13
		dc.b	"Syntax :",10,13
		dc.b	"Compare -{options} {drive:directory}(filename1) {drive:directory}(filename2)",10,13
		dc.b	10,13,10,13
		dc.b	"options are :",10,13
		dc.b	"-h  = comparing hexfiles, just compares the files but doesn't show",10,13
		dc.b	"      where and what the differences are !!",10,13
explaintextend:
diftext:	dc.b	"Difference found at offset : "
numbuffer:	dcb.b	8,32
diftextend:
hextable:	dc.b	"0123456789ABCDEF"
filenotfoundtext:dc.b	"Couldn't find file : "
filenotfoundtextend:
fileopenerror:	dc.b	"Couldn't open file : "
fileopenerrorend:
difsizetext:	dc.b	"Files are different sizes !!!",10,13
		dc.b	"Do you whish to compare anyway ??? (Y/N) : "
difsizetextend:
filescompare:	dc.b	"Files are identical !!",10,13
filescompareend:
fileserrorcompare:	dc.b	"Error while reading so files couldn't be compared",10,13
fileserrorcompareend:	
filesnotcompare:	dc.b	"Files are not identical !!",10,13
filesnotcompareend:
contcomparetext:	dc.b	"More as 15 differences found",10,13
			dc.b	"Do you want to continue ? (Y/N) : "
contcomparetextend:
introtext1:	dc.b	$c,"Comparing » "
introtext1end:
introtext2:	dc.b	10,13,"and       » "
introtext2end:
clearscreen:	dc.b	12
enter:		dc.b	10,13
		dc.b	10,13

 section Mainbss,bss
startbss:
fileinfo:	ds.b	260
dosb:		ds.l	1
outputhandle:	ds.l	1
tempread1:	ds.l	1
tempread2:	ds.l	1
totread1:	ds.l	1
totread2:	ds.l	1
filename1:	ds.l	1
filename2:	ds.l	1
filesize1:	ds.l	1
filesize2:	ds.l	1
filelock1:	ds.l	1
filelock2:	ds.l	1
filenamesize1:	ds.w	1
filenamesize2:	ds.w	1
filehandle1:	ds.l	1
filehandle2:	ds.l	1
filebuffer1:	ds.b	4096
filebuffer2:	ds.b	4096
hexdisp:	ds.b	1
equal:		ds.b	1
totalblockreads:	ds.w	1
tmpbuffer:	ds.b	2
difshow:	ds.w	1
endbss:
