;[]-------------------------------------------------------------------------[]
;|	Program name: 3_3_1.ASM                                              |
;|	Image   file: 3_3_1.COM                                              |
;|	Note        : This is a TSR program.                                 |
;|                    It can record filenames to name_buf and use INT 05H to |
;|                      display them.                                        |
;[]-------------------------------------------------------------------------[]


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

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

old_int05	dd	?
old_int21	dd	?
old_int60	dd	?
old_int61	dd	?
busy_05		db	0

name_ptr	dw      name_buf
name_buf	db	1000H dup (0),0FFH

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

DEBUG	EQU	0
include	register.inc
include	interrup.inc
include	display.inc
include	debug.inc

;------- INT 05H -----------------------

new_int05	proc	far

	cmp	cs:busy_05,1		; Test for INT 05H busy
	je	i05_done		; If busy then branch

	sti				; Enable interrupt
	mov	cs:busy_05,1		; Set INT 05H busy

	pushf				; Execute Old INT 05H routine
	call	cs:old_int05		;

	call	dis_name		; Display buffer

	mov	cs:busy_05,0		; Reset INT 05H idle

i05_done:
	iret				; Return calling program

new_int05	endp

;---------------------------------------
; Display name_buf
;---------------------------------------

dis_name	proc	near

	pushall				; Save all the register values

	mov	ax,cs			; Adjust DS, ES
	mov	ds,ax
	mov	es,ax

	dstring	name_buf		; Use dstring macro to display name_buf

	popall				; Restore all the register values

	ret				; Return

dis_name	endp

;------- INT 21H -----------------------

new_int21	proc	far

	pushall				; Save all the register values
	sti

	mov	ax,cs			; Adjust DS, ES
	mov	ds,ax
	mov	es,ax
	get_ax				; Restore AX

	cmp	ah,3DH			; Is it a open file request ?
	je	i21_1			; If yes, record its name

	cmp	ah,4EH			; Is it a find file request ?
	je	i21_1			; If yes, record its name

	jmp	i21_0			; Do nothing otherwise
i21_1:
	call	record_file		; Record ASCII file name
i21_0:
	popall				; Restore all the register values
	jmp	cs:old_int21		; Jump to execute DOS function call

new_int21	endp

;---------------------------------------
; Record ASCII file name to name_buf.
;---------------------------------------

record_file	proc	near

	mov	di,name_ptr		; Get the pointer of name_buf

	get_ds				; Get old DS value
	mov	ds,ax			; Set DS to old DS value
	get_dx				; Get old DX value
	mov	si,ax			; Set SI to old DX value
rf1:
	cmp	word ptr es:[di],0FFH	; Does the name_buf use up ?
	je	rf3			; If yes, don't record any more
	lodsb				; Let AL get one ASCII character
	cmp	al,0			; Is it the end of the file name ?
	je	rf2			; If yes, put <Return> to name_buf
	stosb				; If not, put it to name_buf
	jmp	rf1			; Repeat this routine again
rf2:
	mov	al,0AH			; Put <Return> to name_buf
	stosb
	mov	al,0DH
	stosb
rf3:
	mov	cs:name_ptr,di		; Let name_ptr hold the pointer

	ret				; Return

record_file	endp

;------- INT 60H -----------------------

new_int60	proc	far

	assume	cs:cseg,ds:nothing	; Override segment

	sti
	pushr	<ax,dx,ds>

	set_int	05H,<word ptr old_int05>,<word ptr old_int05[2]>
	set_int	21H,<word ptr old_int21>,<word ptr old_int21[2]>
	set_int	60H,<word ptr old_int60>,<word ptr old_int60[2]>
	set_int	61H,<word ptr old_int61>,<word ptr old_int61[2]>

	popr	<ds,dx,ax>
	iret

new_int60	endp

;------- Main program ------------------

initialize:

	assume	cs:cseg,ds:cseg

	get_int	05H,old_int05
	set_int	05H,<offset new_int05>
	get_int	21H,old_int21
	set_int	21H,<offset new_int21>
	get_int	60H,old_int60
	set_int	60H,<offset new_int60>
	get_int	61H,old_int61
	set_int	61H,<offset new_int61>

	mov	dx,offset initialize	; Terminate and stay resident
	int	27H

cseg	ends
	end	start
