;[]-------------------------------------------------------------------------[]
;|	Program name: 5_3_1.ASM                                              |
;|	Image   file: 5_3_1.COM                                              |
;|	Note        : Hole Lock protection program.                          |
;|                    ( checking the count of INT 13H )                      |
;[]-------------------------------------------------------------------------[]


cseg	segment
	assume	cs:cseg,ds:cseg
	org	100H
start:
	jmp	initialize

;------- Data area ---------------------

no_use		db	?
cs_ip		label	dword
		dw	offset terminate
		dw	-10H
hole_CX		dw	2205H
hole_DX		dw	0100H
buffer		dw	offset tail
buffer1		dw	offset tail + 200H
err_msg		db	'No key disk!',0DH,0AH,'$'
ter_msg		db	'Protection program terminate!',0DH,0AH,'$'
PSP		dw	0

old_int13	dd	?
count_int13	db	5		; total 5 times request for INT 13H

;------- Include area ------------------

include	register.inc
include	interrup.inc

;------- Macro area --------------------

sec_io	macro	mode,buf
	mov	ax,mode
	mov	bx,buf
	mov	cx,hole_CX
	mov	dx,hole_DX
	int	13H
	endm

;------- INT 13H -----------------------

new_int13	proc	far

	dec	cs:count_int13		; decrease counter
	jmp	cs:old_int13		; execute INT 13H function

new_int13	endp

;------- Check slot of key disk --------

check_hole	proc	near

	push	es			; set INT 13H vector to our new_int13
	get_int	13H,old_int13
	set_int	13H,<offset new_int13>
	pop	es

	mov	ax,0			; reset disk
	int	13H

	mov	ax,0201H		; read a normal sector !!!
	mov	bx,buffer
	mov	cx,0001H
	mov	dx,0000H
	int	13H

	sec_io	0201H,buffer		; read hole sector
	cmp	ax,1001H
	jne	hole_err

	sec_io	0301H,buffer		; write hole sector
	cmp	ax,0001H
	jne	hole_err

	sec_io	0201H,buffer1		; read hole sector
	cmp	ax,1001H
	jne	hole_err

	cld
	mov	cx,0200H		; compare read & write data
	mov	si,buffer
	mov	di,buffer1
	repe	cmpsb
	jcxz	hole_err

	mov	cx,400H			; clear track data in RAM
	xor	ax,ax
	mov	di,buffer
	rep	stosb

	cmp	count_int13,0		; check the counter value
	jne	hole_err

	set_int	13H,<word ptr cs:old_int13>,<word ptr cs:old_int13[2]>

	ret

hole_err:

	set_int	13H,<word ptr cs:old_int13>,<word ptr cs:old_int13[2]>

	mov	ax,cs			; show 'No key disk!' message
	mov	ds,ax
	mov	dx,offset err_msg
	mov	ah,09H
	int	21H

	mov	ah,4CH
	int	21H

check_hole	endp

;------- Initial procedure -------------

initialize:

	mov	cs:PSP,ds
	mov	ax,cs
	mov	ds,ax
	mov	es,ax

	call	check_hole

	mov	ax,cs:PSP		; set cs:ip
	mov	ds,ax
	mov	es,ax
	add	ax,10H
	add	word ptr cs:cs_ip[2],ax
	jmp	cs:cs_ip

terminate:

	mov	ax,cs
	mov	ds,ax
	mov	dx,offset ter_msg
	mov	ah,09H
	int	21H

	mov	ah,4CH
	int	21H

tail:

cseg	ends
	end	start
