.386
.model	tiny,c
.stack	0

dlink	segment	use16
assume	cs:dlink,ds:dlink,es:dlink
org	100h

pszmax		equ	128
pmeret		equ	4096

prog:		jmp	near ptr progr

jelzo		db	0
mennyit		dd	0
ifile		dw	0
ofile		dw	0
iseg		dw	0
ioffs		dw	0
oseg		dw	0
ooffs		dw	0
pszam		dw	0
htx1		db	"Nincs el‚g szabad t r.",13,10,36
htx2		db	"Hi nyz¢ forr s.",13,10,36
htx3		db	"Hib s vagy nem l‚tez“ <$"
htx4		db	"> file.",13,10,36
htx5		db	"Rendben.",13,10,36
pseg		dw	pszmax dup(0)

progr:		push	512
		popf
		mov	bx,65535/16+1
		mov	ah,4ah
		int	21h
		xor	si,si
		mov	cx,pszmax
fogl:		mov	bx,pmeret/16+1
		mov	ah,48h
		int	21h
		jc	hiell
		mov	pseg[si],ax
		inc	si
		inc	si
		mov	es,ax
		xor	eax,eax
		mov	di,ax
		push	cx
		mov	cx,pmeret/4+1
		rep	stosd
		pop	cx
		loop	fogl
hiell:		lea	dx,htx1
		or	si,si
		jz	hiba
		mov	pszam,si
		lea	si,[80h]
		mov	cl,[si]
		lea	dx,htx2
		jcxz	hiba
		inc	si
		inc	si
		dec	cx
		mov	dx,si
		lea	di,progr
		push	ds
		pop	es
		rep	movsb
		mov	dword ptr [si],"gsd."
		mov	[si+4],cl
		movsb
		mov	dword ptr [di],"knl"
		mov	ax,3d00h
		int	21h
		jnc	hibok
fhiba:		lea	si,[82h]
		jmp	hhiba
chiba:		lea	si,progr
hhiba:		lea	dx,htx3
		mov	ah,9
		int	21h
		mov	cx,64
		mov	ah,2
hikir:		mov	dl,[si]
		or	dl,dl
		jz	hixir
		int	21h
		inc	si
		loop	hikir
hixir:		lea	dx,htx4
hiba:		mov	ah,9
		int	21h
		int	20h
hibok:		mov	ofile,ax

iprob:		call	inic
proba:		call	olvalt
		jc	proveg
		call	irseg
		jnc	proba
		mov	bx,ofile
		mov	cx,-1
		mov	dx,cx
		mov	ax,4201h
		int	21h
		jc	fhiba
		or	jelzo,1
		jmp	onzar
proveg:		mov	bx,ofile
		mov	ah,3eh
		int	21h
onzar:		test	jelzo,2
		jnz	letre
		xor	cx,cx
		lea	dx,progr
		mov	ah,3ch
		int	21h
		jc	chiba
		mov	ifile,ax
		or	jelzo,2
letre:		call	olvseg
		jc	lerveg
		mov	ds:[81h],al
		lea	dx,[81h]
		mov	cx,1
		mov	bx,ifile
		mov	ah,40h
		int	21h
		jnc	letre
lerveg:		test	jelzo,1
		jnz	iprob
		and	jelzo,not 2

		mov	bx,ifile
		mov	ah,3eh
		int	21h
		lea	dx,htx5
		jmp	hiba

inic:		xor	eax,eax
		mov	mennyit,eax
		mov	dword ptr iseg,eax
		mov	dword ptr oseg,eax
		and	jelzo,not 1
		ret

olvalt:		mov	bx,ofile
		mov	cx,1
		lea	dx,[81h]
		mov	ah,3fh
		int	21h
		jc	tolveg
		cmp	al,1
		jb	tolveg
		mov	al,ds:[81h]

		cmp	al,"["
		je	megje
		cmp	al,"{"
		jne	nemje
megje:		add	al,2
		xchg	ax,bx
megev:		push	bx
		mov	bx,ofile
		mov	cx,1
		lea	dx,[81h]
		mov	ah,3fh
		int	21h
		pop	bx
		jc	tolveg
		cmp	al,1
		jb	tolveg
		mov	al,ds:[81h]
		cmp	al,bl
		jne	megev
		jmp	olvalt
nemje:
		cmp	al,"\"
		jne	olveg
ujfile:		mov	cx,64
		inc	dx
		mov	ah,3fh
		int	21h
		jc	fhiba
		mov	di,dx
		mov	cx,ax
		jcxz	megvan
megcikl:	cmp	byte ptr [di],33
		jb	megvan
		inc	di
		loop	megcikl
megvan:		mov	[di],ch
		mov	ah,3eh
		int	21h
		mov	ax,3d00h
		int	21h
		jc	fhiba
		mov	ofile,ax
		jmp	olvalt

irseg:		mov	bx,iseg
		cmp	bx,pszam
		jae	irosz
		mov	es,pseg[bx]
		mov	di,ioffs
		stosb
		cmp	di,pmeret
		jb	irben
		add	iseg,2
		xor	di,di
irben:		mov	ioffs,di
		inc	mennyit
olveg:		clc
tolveg:		ret
irosz:		stc
		ret

olvseg:		mov	bx,oseg
		cmp	bx,pszam
		jae	irosz
		cmp	mennyit,0
		je	irosz
		mov	fs,pseg[bx]
		mov	si,ooffs
		lodsb	fs:[si]
		cmp	si,pmeret
		jb	orben
		add	oseg,2
		xor	si,si
orben:		mov	ooffs,si
		dec	mennyit
		clc
		ret
szvege:
dlink	ends

end	prog
