	title	zero memory
;++
;
; This is an attempt to make programs with flakey bugs crash more predictably.
;
; It sets all available memory to zero, or to the hex # specified as JCL.
;
; 04/02/92	JMBW	Created.
;
;--
code	segment
	assume	cs:code
	org	80h
buf:	; relocate clear loop to here
	org	100h
start:	cld			;forwards
	mov	si,81h		;pt at JCL
	mov	cx,4		;value,,bit count
lp1:	; parse JCL as a hex number (value to fill with, default is 0)
	lodsb			;get a byte
	cmp	al,15q		;cr?
	je	msize
	sub	al,'0'		;see if hex
	cmp	al,9d		;0-9?
	jbe	sk2
	sub	al,'A'-'0'	;A-F?
	cmp	al,5
	jbe	sk1
	sub	al,'a'-'A'	;a-f?
	cmp	al,5
	ja	lp1		;ignore if not (spaces, junk)
sk1:	add	al,10d		;A-F => 10d-15d
sk2:	sal	ch,cl		;left 4
	or	ch,al		;OR in new digit
	jmp	short lp1	;loop
msize:	; find size of memory to zero (put # bytes in dx:ax)
	mov	ax,word ptr ds:[2] ;get end of memory
	mov	bx,ds		;pt at PSP
	add	bx,10h+10h	;pt at program+100h
	sub	ax,bx		;find # paragraphs to clear
	xor	dx,dx		;dx=0
	mov	bl,ch		;save
	mov	bh,ch		;on both sides
	xor	ch,ch		;ch=0
lp2:	sal	ax,1		;*16d=byte count
	rcl	dl,1
	loop	lp2		;loop
	; relocate the fill loop into the PSP so we can get everything
	xchg	ax,bx		;get fill word, set count in last seg
	mov	si,offset fill1	;point at routine
	mov	di,offset buf	;point at dest
	mov	cx,offset lfill	;length
	rep	movsb		;copy
	; set up and go
	mov	cx,es		;copy es (PSP)
	add	cx,10h+10h	;point at program+100h
	mov	es,cx		;copy
	xor	di,di		;pt at begn
	jmp	buf		;go do it
;
fill1:	mov	sp,200h		;set up stack
fill2:	dec	dx		;done all full 64KB banks?
	js	fill3		;yes
	mov	cx,8000h	;w.c. for 64KB
	rep	stosw		;zero out a page
	mov	cx,es		;copy
	add	ch,10h		;+64KB
	mov	es,cx		;restore
	jmp	short fill2	;loop
fill3:	mov	cx,bx		;do last seg
	shr	cx,1		;/2=w.c.
	rep	stosw
	mov	dx,offset done-fill1+80h ;msg
	mov	ah,09h		;func=print
	int	21h		;say we're done
	int	20h
done	db	'Memory zeroed',15q,12q,'$'
lfill	equ	$-fill1
;
code	ends
	end	start

