home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / spo386.zip / INT_0405.PAS < prev    next >
Pascal/Delphi Source File  |  1994-01-13  |  6KB  |  198 lines

  1. Unit      Int_0405; Interface
  2. { ------------------------------------------------------------------------- }
  3. { Support module for units optimized by Sally Peephole Optimizer.           }
  4. { Copyright 1994 by Sally I/S.                                              }
  5. {                                                                           }
  6. { Private individuals may distribute compiled versions of this unit as part }
  7. { of their own software provided that the copyright messages are left       }
  8. { intact.                                                                   }
  9. {                                                                           }
  10. { Installs handlers for exceptions 04 and 05.  These are generated by the   }
  11. { IntO and Bound instructions.  As IBM used 05 for print screen despite its }
  12. { being reserved by Intel we check for the origin of the interrupt -- it's  }
  13. { fool proof but it's not damn fool proof.                                  }
  14. { ------------------------------------------------------------------------- }
  15. {$R-,S-,I-,Q-,O-,F-,G-,A+}
  16. {$IFNDEF MSDOS}
  17.   {$C Fixed Preload Permanent} { Holds exception handlers }
  18. {$ENDIF}
  19.  
  20. Implementation
  21.  
  22. Uses      {$IFDEF WINDOWS} Windos {$ENDIF}
  23.           {$IFDEF OS2}     Windos {$ENDIF}
  24.           {$IFDEF DPMI}    Dos    {$ENDIF}
  25.           {$IFDEF MSDOS}   Dos    {$ENDIF};
  26. { ------------------------------------------------------------------------- }
  27. Var       Halterror  : Pointer;
  28.           SaveInt04  : Pointer;
  29.           SaveInt05  : Pointer;
  30.           SaveExit   : Pointer;
  31. { ------------------------------------------------------------------------- }
  32. {$IFDEF MSDOS}
  33. Procedure Real_Int04; Far; Assembler;
  34. Asm
  35.           Jmp    @@0
  36.   @@1:    Db     0Eah
  37.           Dd     0
  38.   @@0:    Jno    @@1
  39.           Push   Ax
  40.           Mov    Ax,Ds
  41.           Cmp    Ax,Seg @Data
  42.           Pop    Ax
  43.           Jne    @@1
  44.           Mov    Ax,215
  45.           Jmp    [Dword Ptr Halterror]
  46. End;
  47. { ------------------------------------------------------------------------- }
  48. Procedure Real_Int05; Far; Assembler;
  49. Asm
  50.           Jmp    @@0
  51.   @@1:    Db     0Eah
  52.           Dd     0
  53.   @@0:    Push   Ax
  54.           Mov    Ax,Ds
  55.           Cmp    Ax,Seg @Data
  56.           Pop    Ax
  57.           Jne    @@1
  58.           Push   Bp
  59.           Mov    Bp,Sp
  60.           Lds    Bp,[Bp+2]
  61.           Cmp    [Word Ptr Ds:Bp],$622E          { Bound Ax,[Cs:....] }
  62.           Je     @@5
  63.           Cmp    [Word Ptr Ds:Bp],$2E66          { Op32, Cs: }
  64.           Je     @@3
  65.           Cmp    [Word Ptr Ds:Bp],$662E          { Cs:, Op32 }
  66.           Jne    @@2
  67.   @@3:    Cmp    [Word Ptr Ds:Bp+2],$0662        { Bound Eax,[....] }
  68.           Jne    @@2
  69.   @@5:    Mov    Bp,Seg @Data
  70.           Mov    Ds,Bp
  71.           Pop    Bp
  72.           Mov    Ax,201
  73.           Jmp    [Dword Ptr Halterror]
  74.   @@2:    Mov    Bp,Seg @Data
  75.           Mov    Ds,Bp
  76.           Pop    Bp
  77.           Jmp    @@1
  78. End;
  79. {$ENDIF MSDOS}
  80. { ------------------------------------------------------------------------- }
  81. {$IFNDEF MSDOS}
  82. Procedure Protected_Except04; Far; Assembler;
  83. Asm       Push   Ax
  84.           Mov    Ax,215
  85.           Db     0Eah
  86.           Dd     0
  87. End;
  88. { ------------------------------------------------------------------------- }
  89. Procedure Protected_Except05; Far; Assembler;
  90. Asm       Push   Ax
  91.           Mov    Ax,201
  92.           Db     0Eah
  93.           Dd     0
  94. End;
  95. {$ENDIF MSDOS}
  96. { ------------------------------------------------------------------------- }
  97. Procedure NewExit; Far;
  98. Begin
  99.   ExitProc:=SaveExit;
  100.   {$IFDEF MSDOS}
  101.     SetIntVec($04,SaveInt04);
  102.     SetIntVec($05,SaveInt05);
  103.   {$ELSE}
  104.     Asm    Mov   Ax,0203H
  105.            Mov   Bl,04H
  106.            Mov   Cx,[Word Ptr SaveInt04+2]
  107.            Mov   Dx,[Word Ptr SaveInt04]
  108.            Int   31H
  109.  
  110.            Mov   Ax,0203H
  111.            Mov   Bl,05H
  112.            Mov   Cx,[Word Ptr SaveInt05+2]
  113.            Mov   Dx,[Word Ptr SaveInt05]
  114.            Int   31H
  115.     End;
  116.   {$ENDIF}
  117. End;
  118. { ------------------------------------------------------------------------- }
  119. Procedure Install;
  120. Var       Where     : Pointer;
  121.           Copyright : String;
  122. Begin
  123.   Copyright:='Portions copyright 1993 by Sally I/S'#13#10;
  124.   SaveExit:=ExitProc;
  125.   ExitProc:=@NewExit;
  126.  
  127.   {$IFDEF MSDOS}
  128.     GetIntVec($04,SaveInt04);          { Save state }
  129.     GetIntVec($05,SaveInt05);
  130.     SetIntVec($04,@Real_Int04);
  131.     SetIntVec($05,@Real_Int05);
  132.  
  133.     Where:=@Real_Int04;                { Place handlers for easy access. }
  134.     Inc(Word(Where),3);
  135.     Pointer(Where^):=SaveInt04;
  136.     Where:=@Real_Int05;
  137.     Inc(Word(Where),3);
  138.     Pointer(Where^):=SaveInt05;
  139.  
  140.     GetIntVec($00,Where);              { Dirty hack to find HaltError. }
  141.     Inc(Word(Where),3);
  142.     Halterror:=Where;
  143.   {$ELSE}
  144.     Asm    Mov   Ax,0202H
  145.            Mov   Bl,04H
  146.            Int   31H
  147.            Mov   [Word Ptr SaveInt04],Dx
  148.            Mov   [Word Ptr SaveInt04+2],Cx
  149.  
  150.            Mov   Ax,0202H
  151.            Mov   Bl,05H
  152.            Int   31H
  153.            Mov   [Word Ptr SaveInt05],Dx
  154.            Mov   [Word Ptr SaveInt05+2],Cx
  155.  
  156.            Mov   Ax,0203H
  157.            Mov   Bl,04H
  158.            Mov   Cx,Cs
  159.            Mov   Dx,Offset Protected_Except04
  160.            Int   31H
  161.  
  162.            Mov   Ax,0203H
  163.            Mov   Bl,05H
  164.            Mov   Cx,Cs
  165.            Mov   Dx,Offset Protected_Except05
  166.            Int   31H
  167.  
  168.            Mov   Ax,0202H              { Get and save exception entry }
  169.            Mov   Bl,00H
  170.            Int   31H
  171.            Add   Dx,4
  172.            Mov   Ax,Cs
  173.            Add   Ax,[Selectorinc]
  174.            Mov   Es,Ax
  175.            Mov   [Word Ptr Es:Protected_Except04+5],Dx
  176.            Mov   [Word Ptr Es:Protected_Except04+7],Cx
  177.            Mov   [Word Ptr Es:Protected_Except05+5],Dx
  178.            Mov   [Word Ptr Es:Protected_Except05+7],Cx
  179.     End;
  180.   {$ENDIF}
  181. End;
  182. { ------------------------------------------------------------------------- }
  183. Procedure Zeroextend_Registers;
  184. Const     Op32 = $66;
  185. Begin
  186.   { This zero extends the registers Esp and Ebp for 386/486 processors
  187.     I don't really know if this is needed, but it won't hurt. }
  188.   If Test8086>=2 Then
  189.     Asm    Db Op32 ; And Sp,$FFFF ; Dw 0
  190.            Db Op32 ; And Bp,$FFFF ; Dw 0
  191.     End;
  192. End;
  193. { ------------------------------------------------------------------------- }
  194. Begin
  195.   Zeroextend_Registers;
  196.   Install;
  197. End.
  198.