	org 100h
		jmp begin
fdos		equ 05h
directory	equ 80h
print$char	equ 02h
print$cons	equ 09h
read$cons	equ 10
match$1st	equ 17
match$next	equ 18
renamef		equ 23
disk$reset	equ 13
input$buffer	db  32
		ds  32
fcb		db  0,'???????????'
		dw  0,0,0,0,0,0,0,0,0,0,0
fcb$count	db  0
fcb$location	dw  fcb$buffer
file$count	db  0
file$total	db  0
old$sp		dw  0
stack		ds  32
stack$top	equ $
m1buffer	db  '    A P H  Security System$'
m2buffer	db  '  Closing Directory - Please Wait.$'
m4buffer	db  '  Directory now closed.$'
crlf:		push b
		push d
		push h
		mvi c,print$char
		mvi e,0dh
		call fdos
		mvi c,print$char
		mvi e,0ah
		call fdos
		pop h
		pop d
		pop b
		ret
begin:		lxi h,0
		dad sp
		shld old$sp
		lxi sp,stack$top
		mvi c,disk$reset
		call fdos
		call crlf
		call crlf
		call crlf
		mvi c,print$cons
		lxi d,m1buffer
		call fdos
		call crlf
		call crlf
		mvi c,print$cons
		lxi d,m2buffer
		call fdos
		call crlf
		call crlf
		call crlf
doneit:		mvi c,match$1st
ditlp1:		lxi d,fcb
		call fdos
		cpi 255
		jz all$match
		mvi b,1
		rrc
		jnc no$lsb
		inr b
no$lsb:		rrc
		jnc no$nsb
		inr b
		inr b
no$nsb:		lxi h,directory - 20h
		lxi d,20h
fcblp1:		dad d
		dcr b
		jz fcbset
		jmp fcblp1
fcbset:		lda fcb$count
		inr a
		sta fcb$count
		push h
		lhld fcb$location
		push h
		pop d
		pop h
		lxi b,32
		db  0edh,0b0h
		push d
		pop h
		shld fcb$location
		mvi c,match$next
		jmp ditlp1
all$match:	lda fcb$count
		sta file$total
		lxi h,fcb$buffer
		lxi d,fcb$buffer + 16
sublp0:		mvi c,11
		mvi a,0
		stax d
		inx h
		inx d
sublp1:		mov a,m
		cpi ' '
		jz nosub
		adi 20h
nosub:		stax d
		inx h
		inx d
		dcr c
		jnz sublp1
		mvi a,0
		mvi c,4
nslp:		stax d
		inx d
		inx h
		dcr c
		jnz nslp
		lda fcb$count
		dcr a
		jz rename
		sta fcb$count
		lxi b,16
		dad b
		push h
		push d
		pop h
		dad b
		push h
		pop d
		pop h
		jmp sublp0
rename:		lda file$total
		lxi h,fcb$buffer
renlp1:		push h
		lxi b,12
		mvi e,2
stlp:		dad b
		mvi d,4
		mvi a,0
lp:		mov m,a
		inx h
		dcr d
		jnz lp
		dcr e
		jnz stlp
		pop d
		push d
		mvi c,renamef
		call fdos
		pop h
		lxi d,32
		dad d
		lda file$total
		dcr a
		sta file$total
		jnz renlp1
		mvi c,renamef
		lxi d,spfcb
		call fdos
		jmp donren
spfcb:		db 0,6fh,70h,65h,6eh,20h,20h,20h,20h,63h,6fh,6dh,0,0,0,0,0
		db 'OPEN    COM',0
donren:		call crlf
		call crlf
		mvi c,print$cons
		lxi d,m4buffer
		call fdos
fend:		lhld old$sp
		sphl
		jmp 0000h
fcb$buffer	equ  $
		end 100h
