* This was assembled with GenAm2.14D.   Tabwidth is 10!   :-)

	OPT	C+,D-,O+,P+

*
* Here's another late night hack... I came across an old Fish Disk and
* found a program called MemClear. As I wanted to fool around with memory
* management anyway, I tried to re-code it in assembler. Here's the
* result.
*
* What makes me wonder is that my program *NEVER* counts the same number
* of MemChunks the original counts, although I'm quit sure that I made no
* mistake. If you find a bug within this program, please point it out to
* me at the following address:
*
* Z-NET: P.FROEHLICH@MUSIKBOX.ZER
*
* or post it to Z-NETZ/RECHNER/AMIGA/PROGRAMMIEREN for all to see.
*
* Thanx...
*

	INCDIR	"INC:"
	INCLUDE	"exec/exec.i"
	INCLUDE	"exec/execbase.i"
	INCLUDE	"libraries/dos.i"

	INCLUDE	"misc/Macros.i"	* some useful macros
	INCLUDE	"misc/Lib.i"	* all the _LVO's

	rsreset
Frame	rs.b	0
f_total	rs.l	1
f_count	rs.l	1
f_arp	rs.l	1
f_SIZE	rs.w	0

CALLA	MACRO
	MOVE.L	f_arp(SP),a6
	JSR	_LVO\1(a6)
	ENDM

	SECTION	MemClear,CODE
_main:
	SUB.L	#f_SIZE,SP	* I know, there's LINK/UNLK...
	OPENLIB	_ArpName(PC),f_arp(SP),NoArpLib

	LEA	MemList(a6),a5	* get MemListHeader
	IFEMPTY	a5,Quit		* MemList empty ? -> Weird...
	move.l	LH_HEAD(a5),a5	* get first memheader
	
	CLEAR	d7		* total bytes
	CLEAR	d6		* total memchunks
	MOVEQ	#MC_SIZE,d5	* size of MemChunk overhead
	CLEAR	d2		* for the clear routine

	LEA	TXT_Title(PC),a1
	CALLA	Puts

	CALLEXEC	Forbid
NextMemHeader:
	MOVE.L	MH_FIRST(a5),a4
NextMemChunk:
	MOVE.L	MC_BYTES(a4),d4	* get length of this chunk
	SUB.L	d5,d4		* correct length
	beq.s	TinyChunk		* this chunk contains only
				* itself... we can't clear it

	move.l	a4,a3		* get address of chunk
	add.l	d5,a3		* correct address

	move.l	d4,d3
	lsr.l	#2,d3		* divide by 4 for longs
	DEC.L	d3		* sub 1 because of dbra

Clear:
	MOVE.L	d2,(a3)+		* kinda slow... I know...
	dbra	d3,Clear

TinyChunk:
	ADD.L	d4,d7
	INC.L	d6

	TST.L	MC_NEXT(a4)
	BEQ.S	NoMoreMemChunks
	MOVE.L	(a4),a4
	BRA.S	NextMemChunk
NoMoreMemChunks:
	TSTNODE	a5,a5
	BEQ.S	NoMoreMemHeaders
	BRA.S	NextMemHeader
NoMoreMemHeaders:
	CALLEXEC	Permit

	MOVE.L	d7,f_total(SP)
	MOVE.L	d6,f_count(SP)

	LEA	FMT_Info(PC),a0
	LEA	f_total(SP),a1
	CALLA	Printf

	BRA.S	Quit
_exit:
	ADD.L	#f_SIZE,SP
	RTS
Quit:
	CLOSELIB	f_arp(SP)
NoArpLib:
	MOVEQ	#RETURN_FAIL,d0
	BRA.S	_exit

TXT_Title:
	dc.b	$9B,"33m"
	dc.b	"MemClear"
	dc.b	$9B,"m"
	dc.b	" V2.0 © 1991 by P.Fröhlich, Original © 1986 by John Hodgson.",0
FMT_Info:
	dc.b	"Cleared %ld bytes in %ld MemChunks.",10,0
_ArpName:
	ARPNAME

	END

