	exeobj
	objfile	ram:SelfChecksum

	comment	®

written	23-08-91

	Often it would be usefull to be able to do some sort of checksum
on our code during assembly, so be used while running.

	One instance may be where you are writing resident code and wish
to check it's integrety before execution.  Other instances may be where
you have a disk block hard coded in your program (ala install) and would
like the block's checksum included automatically.  You will undoubtedly
find other uses.

	The FOR...NEXT loop near the end of this source is an example of
how to do this.  Note that this loop can only be successful if placed
AFTER the block being checksum'ed, hence the use of the POKE directive
to place the xsum value near the beginning of the file.

Set tabstops: 8

Assemble with Macro68
®

	ifnd	EXEC_TYPES_I
	if1
	PRINTX	\N\C5;32m*** Resident maclib "AllSyms.mac" not in use. ***\C0;31m\N
	endc
_AbsExecBase		equ	4
_LVOAlert		equ	-108	;exec
_LVOCloseLibrary	equ	-414	;exec
_LVOForbid		equ	-132	;exec
_LVOOpenLibrary		equ	-552	;exec
_LVOPermit		equ	-138	;exec
_LVOFindName		equ	-276	;exec
AT_Recovery		equ	$00000000
AG_OpenLib		equ	$00030000
	endc
	ifnd	csd_DSSBase
_AbsExecBase		equ	4
csd_DSSBase		equ	16
	endc

_LVOdsPrintf	equ	-636

*****************************

		bra.b		START

AnotherXSum:
	dl	0

START:		moveq		#0,d3
		movea.l		(_AbsExecBase).w,a6	;establish exec for calls

	; --- do workbench stuff first --- ABORT if from workbench

		movea.l		(ThisTask,a6),a3
		tst.l		(pr_CLI,a3)
		bne.b		.fromCLI		;branch if from CLI
		lea		(pr_MsgPort,a3),A0	;our process base
		jsr		(_LVOWaitPort,a6)	;wait for message from starter
		lea		(pr_MsgPort,a3),A0	;our process base
		jsr		(_LVOGetMsg,a6)
		movea.l		d0,a1			;workbench message
		jsr		(_LVOReplyMsg,a6)
		moveq		#RETURN_FAIL,d0
		rts

	; --- open the support library

.fromCLI:	moveq		#17,d0			;version #
		lea	 	(supportLibName,pc),a1	;point to liberary name
		jsr		(_LVOOpenLibrary,a6)	;exec
		tst.l		d0
		bne.b		.libOpen
		lea		(intuitionLibName,pc),a1
		jsr		(_LVOOldOpenLibrary,a6)
		tst.l		d0
		bne.b		.intuitionAvailable
		movei.l		#AT_Recovery!AG_OpenLib!AO_Intuition,d7
		jsr		(_LVOAlert,a6)
		bra.b		.failStart
.intuitionAvailable:
		movea.l		d0,a6				;intuition base
		lea		(digisoftLibLost_AlertMsg,pc),a0
		moveq		#0,d0
		moveq		#50,d1
		jsr		(_LVODisplayAlert,a6)
		movea.l		a6,a1
		movea.l		(_AbsExecBase).w,a6
		jsr		(_LVOCloseLibrary,a6)
.failStart:	moveq		#RETURN_FAIL,d0
		rts

	; --- initialize the scratch area

.libOpen:	movea.l		d0,a6
		push		(XsumValue,pc)
		lea		(FormatString,pc),a0
		movea.l		sp,a1
		jsr		(_LVOdsPrintf,a6)
		addq.l		#4,sp

		push		(AnotherXSum,pc)
		lea		(FormatString,pc),a0
		movea.l		sp,a1
		jsr		(_LVOdsPrintf,a6)
		addq.l		#4,sp

Exit_To_DOS:	movea.l		a6,a1
		movea.l		(_AbsExecBase).w,a6
		jsr		(_LVOCloseLibrary,a6)
		moveq		#0,d0
		rts

*****************************

supportLibName:
	cstr	'digisoftSupport.library'
intuitionLibName:
	cstr	'intuition.library'


	oddok
digisoftLibLost_AlertMsg:
	dw	137
	db	15
	cstr		'digisoftSupport.library ----------- LOST'
	db	-1
	dw	43
	db	35
	cstr		'LEFT = OK'
	db	-1
	dw	640-123
	db	35
	cstr		'RIGHT = OK',0
	odderror



FormatString:
	cstr	'The checksum for this code is %ld.',Lf

	even
FINISH:

xsum	set	0	;so Macro68 sees this symbol defined on pass 1
temp	set	0	;so Macro68 sees this symbol defined on pass 1

	if2					;pass 2 ONLY since we use "_hunkbase"
	FOR temp = START-START TO FINISH-START STEP 2
xsum	set	xsum+_peekw(_hunkbase+temp)	;our xsum is additive word -> longword
	NEXT
	endc

	POKE.L	#xsum,(_hunkbase+AnotherXSum)

XsumValue:
	dl	xsum

*****************************

	END
