*
*	CheckRange.asm (of the PCQ Pascal runtime library)
*	Copyright (c) 1989 Patrick Quaid
*
*	This routine makes sure an array index is within its
*	proper bounds.  This used to be handled in-line, but
*	I wanted to expand the error checking mechanism a bit.
*

*	On entry, d0 is the 32-bit index.  On top of the stack is
*	the maximum value, and below that is the minimum.  Note:
*	To save as much code for the caller as possible, this routine
*	cleans up the callers stack.   Note that this routine uses
*	a0, which according to the normal call from Calls.p should
*	be OK.

	XREF	_p%exit
	XREF	_ExitAddr

	XDEF	_p%CheckRange
_p%CheckRange
	cmp.l	8(sp),d0		; compare lower & index
	blt.s	BadIndex		; too low
	cmp.l	4(sp),d0		; compare upper & index
	bgt.s	BadIndex		; too high
FixStack
	move.l	(sp),a0			; get return address
	adda.l	#12,sp			; get return, plus 2 ops
	move.l	a0,-(sp)		; push back return
	rts
BadIndex
	move.l	(sp),_ExitAddr		; address of CALLER
	move.l	#60,d0			; set up for call
	jsr	_p%exit			; blow up
	bra	FixStack		; we might return...

	END
