;@GOTO TRANSLATE

.586P
.MODEL FLAT

 INCLUDE EXD.INC

.CODE

TotalStart:                                   ;start of FLAT image for _HeapFree


        MySign   EQU   98979695H              ;for Uninstall
        _HLT     EQU   0F4H


       CheckAPI:
        CMP  [EBP].Client_EBP, MySign         ;is in V86 EBP "my" value
        JNE  NotMe13                          ;no -> ignore
 
        ;uninstall
	LEA   ESI,MainInt13
	MOV   EAX,13
	VMMCall UnHook_V86_Fault              ;try to unhook
        JC    Quit                            ;failed -> return Carry set

        INC   [EBP].Client_EIP                ;V86 IP to the following
                                              ;instruction 

	PUSH  0 OFFSET TotalStart             ;parameters for _HeapFree

        ;copy "freemem" routine to another place (DOS stack)
        ;because it's risky to free space where I'm running

        MOV    AX, Client_SS * 256 + Client_ESP
        VMMCall Map_Flat
        CMP   EAX, -1
        JE    Quit
        XCHG  EAX,EDI   
	LEA   ESI,QuitCode
	MOV   ECX,QuitCodeL
	CLD
	SUB   EDI,ECX
	AND   BYTE PTR [EBP].Client_Eflags,NOT 1  ;success -> clear Carry flag
	PUSH  EDI
	REP   MOVSB                           ;copy to DOS stack

	RET   ;goto DOS stack

        ;this "freemem" routine is placed and executed in DOS stack

      QuitCode:
	VMMCall _HeapFree
	ADD   ESP,8                           ;two parameters of _HeapFree   
      Quit:
	POPAD
	POPFD
      UnInstFailed:
	RET
      QuitCodeL EQU $-QuitCode

;-------------------------------------------------------------------------------

        ;Following jumps are required  ("handler header") by Install_???_Hook
        ;(trace Install_???_Hook in WinICE) to get PrevHook13 address

	JMP	MainINT13             ;EB 06  (can be EB 0A too)

      Chain13:
	JMP	PrevHook13            ;FF 25 AdrOfPrev
                                      ;Hook will fill [AdrOfPrev] 

      MainINT13:                      ;handler begins here!!!

      ;in EBX is (as always) handle of Virtual Machine which caused exception 13
      ;in EBP is (as always) pointer to Client_Reg_Struc

	PUSHFD
	PUSHAD

	CLD
        MOV    AX, Client_CS * 256 + Client_EIP
        VMMCall Map_Flat
        CMP   EAX, -1
        JE    Quit
        XCHG    EAX,ESI
        MOV     EBX,ESI               

        CMP     BYTE PTR [ESI],_HLT   ;was it HLT (API?)
        JE      CheckAPI

        ;-------skip prefixes
       NextPref:
	LODSB
	LEA	EDI,PrefixTable       ;contains prefixes(without LOCK) + 0FH
	MOV	ECX,PrefixTableL
	REPNE	SCASB
	JE	NextPref

        MOV     Rxx, AL

        AND     AL, NOT 7             

        CMP     AL,  30H
        JNE     CheckXRX 
         MOV     Nopx,90H 
         JMP     RestoreReg

       CheckXRX:
        CMP     AL,  20H
        JNE     NotMe13 
         LODSB
         MOV     Nopx,AL 
         JMP     RestoreReg

       RestoreReg:
        SUB     ESI,EBX               ;EBX = original IP
	ADD	[EBP].Client_EIP,ESI  ;V86 IP to the next instruction

        MOV     EAX,[EBP].Client_EAX
        MOV     ECX,[EBP].Client_ECX
        MOV     EDX,[EBP].Client_EDX
        MOV     EBX,[EBP].Client_EBX
        MOV     ESI,[EBP].Client_ESI
        MOV     EDI,[EBP].Client_EDI
        PUSH    EBP 
        MOV     EBP,[EBP].Client_EBP

                DB 0FH
        Rxx     DB 31H
        Nopx    DB 90H

        JMP     $+2

        MOV     RetEBP, EBP
        POP     EBP
        MOV     [EBP].Client_EAX,EAX
        MOV     [EBP].Client_ECX,ECX
        MOV     [EBP].Client_EDX,EDX
        MOV     [EBP].Client_EBX,EBX
        MOV     [EBP].Client_ESI,ESI
        MOV     [EBP].Client_EDI,EDI
        MOV     EAX, RetEBP
        MOV     [EBP].Client_EBP,EAX
        JMP     Quit

     NotMe13:                         ;it was not MOV from/to CR/DR or "my" HLT
	CMP	PrevHook13,0          ;no previous hook?
	JE	Quit                  ;yes -> quit RET
	POPAD
	POPFD
	JMP	Chain13               ;no -> chain

PrefixTable	DB 26H,2EH,36H,3EH,64H,65H,66H,67H,0F2H,0F3H,  0FH
PrefixTableL	EQU $-PrefixTable


Start:
	LEA     ESI,MainINT13
	MOV     EAX,13
	VMMCall Hook_V86_Fault
        ;returns CF according to result so there's no need to set/clear CF
        ;explicitly
        RET

.DATA
	PrevHook13  DD ?
        RetEBP      DD ?
.STACK	4

END Start

:TRANSLATE
@ECHO OFF
CALL  ..\..\ExDLoad\ExDLdr.bat ..\..\ExDLoad\ .\ ..\..\Include\
SET APP=Emu4W9x
TASM  /M /T /Q /I..\..\Include %APP%.BAT
DLINK -SExDLdr %APP% >NUL
DEL            %APP%.obj
DEL            ExDLdr.exe
SET APP=