;[]-------------------------------------------------------------------------[]
;|	Program name: 5_2_3.ASM                                              |
;|	Image   file: 5_2_3.COM                                              |
;|	Note        : The program protection code.                           |
;|		      There are some bugs in this program.                   |
;[]-------------------------------------------------------------------------[]


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
slot_ptr	dw	179AH
slot_data	db	4EH,8EH,4EH,4EH
buffer		dw	offset tail
err_msg		db	'No key disk!',0DH,0AH,'$'
ter_msg		db	'Protection program terminate!',0DH,0AH,'$'
PSP		dw	0

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

check_slot	proc	near

	mov	ax,0			; reset disk
	int	13H

	xor	ax,ax			; set disk parameter
	mov	es,ax
	mov	byte ptr es:[0525H],6

	mov	ax,0201H		; read track
	mov	bx,buffer
	mov	cx,2801H
	mov	dx,0000H
	push	cs
	pop	es
	int	13H

	mov	ax,0			; restore disk parameter
	mov	es,ax
	mov	byte ptr es:[0525H],2

	jnc	slot_err		; check disk controller state
	mov	ah,01
	int	13H
	cmp	ah,10H
	jne	slot_err

	mov	bx,cs:slot_ptr		; get track slot and original cs:ip
	add	bx,buffer
	mov	ax,cs:[bx]
	cmp	word ptr cs:slot_data,ax
	jne	slot_err
	mov	ax,cs:[bx+2]
	cmp	word ptr cs:slot_data[2],ax
	jne	slot_err

	push	cs			; clear track data in RAM
	pop	es
	mov	cx,8192
	xor	ax,ax
	mov	di,buffer
	cld
	rep	stosb

	ret

slot_err:

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

	mov	ah,4CH
	int	21H

check_slot	endp

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

initialize:

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

	call	check_slot

	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