_TEXT	SEGMENT  BYTE PUBLIC 'CODE'
_TEXT	ENDS

	assume	nothing
	ASSUME  CS: _TEXT

_TEXT	SEGMENT

DoInt24	PROC FAR
	push	bp
	mov	bp,sp
	pushf
	push	ax
	mov	ax,[bp+8]	;get function code from stack
	cmp	ah,3Fh
	je	IsRead
	cmp	ah,40h
	jne	NotRW
IsRead:
IsWrite:
	mov	ax,[bp+0Ah]	;get handle from stack
	cmp	ax,cs:[MyHandle]
	jne	NotRW
	pop	ax
	popf
	pop	bp
	mov	al,3		;force FAILURE
	iret	
NotRW:	pop	ax
	popf
	pop	bp
	db	0EAh		;far jump
OI24Offset  dw	0
OI24Segment dw	0
MyHandle    dw	?
DoInt24	ENDP

;void near crit_on(handle)
;  int handle;
	public	_crit_on
_crit_on proc	near
	push	bp
	mov	bp,sp
	mov	ax,[bp+4]	;pick up the handle
	mov	cs:[MyHandle],ax	;and save it
	cmp	cs:[OI24Offset],0	;check if this is the first time
	jne	nosave
	cmp	cs:[OI24Segment],0
	jne	nosave

save:	mov	ax,3524h	;get old Int24 vector
	int	21h
	mov	cs:[OI24Offset],bx
	mov	cs:[OI24Segment],es

nosave:	push	ds
	push	cs
	pop	ds
	mov	dx,offset DoInt24	;ds:dx now points to DoInt24
	mov	ax,2524h		;set Int24 vector
	int	21h
	pop	ds
	pop	bp
	ret
_crit_on endp

;void near crit_off()
	public	_crit_off
_crit_off proc	near
	cmp	cs:[OI24Offset],0
	jne	get
	cmp	cs:[OI24Segment],0
	je	noget
get:	push	ds
	mov	dx,cs:[OI24Offset]
	mov	ds,cs:[OI24Segment]
	mov	ax,2524h
	int	21h
	pop	ds
noget:	ret
_crit_off endp 
	
_TEXT	ENDS
END

