home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / b / bc3.zip / XCEPT.CPP < prev    next >
C/C++ Source or Header  |  1992-02-13  |  3KB  |  81 lines

  1. // (C) Copyright 1992 Qualitas, Inc.  All rights reserved.
  2. //
  3. // xcept.cpp - example usage of exception class in DPMI class library
  4. //
  5.  
  6. #include <iostream.h>
  7. #include <iomanip.h>
  8. #include "dpmihost.h"
  9. #include "exceptio.h"
  10. #include "segment.h"
  11.  
  12.  
  13. ExceptionHandler* pNPhandler;
  14. MemorySegment* pMemSeg;
  15.  
  16. // Handler for Segment-Not-Present exceptions
  17. //
  18. // This handler serves to illustrate how to implement exception handlers.
  19. // In order to run on a local stack, it is critical to call the enter
  20. // member of the ExceptionHandler class immediately upon entry to the 
  21. // handler.  
  22. //
  23. // Although this example does not do so, it is possible to modify the
  24. // values in the excFrame structure (fault CS:IP, flags, SS:SP). By
  25. // doing so it is possible for the exception handler to alter the main 
  26. // thread of execution.
  27. //
  28. void handleNotPresentException(dpmiRegs_t regs, excFrame ef)
  29. {
  30.     cout << "Segment not present: selector=" << hex << ef.excError
  31.          << "\n";
  32.     cout << "Fault address is " << ef.excCS << ":" << ef.excIP << "\n";
  33.     cout << " ax=" << regs.drEAX.s << " bx=" << regs.drEBX.s;
  34.     cout << " cx=" << regs.drECX.s << " dx=" << regs.drEDX.s;
  35.     cout << " si=" << regs.drESI.s << " di=" << regs.drEDI.s << "\n";
  36.     cout << " ds=" << regs.drDS << " es=" << regs.drES;
  37.     cout << " ss=" << ef.excSS  << " sp=" << ef.excSP << "\n";
  38.  
  39.     *pMemSeg + present;        // make it present
  40. }
  41.  
  42. // This function illustrates the use of exception handlers.  It creates
  43. // an handler for exception 0xB - segment not present.  It then creates
  44. // a memory segment, and makes it not present.  Attempting to access the
  45. // not present segment results in an exception.  The DPMI host invokes
  46. // the installed exception handler, which makes the memory segment
  47. // present.  When it returns, the faulting instruction completes
  48. // successfully.
  49. //
  50. void TestExceptions(void)
  51. {
  52.     char far *p;
  53.  
  54.     pNPhandler = new ExceptionHandler(0xB, handleNotPresentException);
  55.                         // install the handler
  56.  
  57.     pMemSeg = new MemorySegment(0x100);    // allocate a memory block
  58.     *pMemSeg - present;            // make it not present
  59.     
  60.     p = (char far *)pMemSeg->ptrTo();    // get a pointer to the memory
  61.  
  62.     *p = 0x77;                // touch it to cause fault
  63.     if (pMemSeg->queryProp(present))
  64.         cout << "Success\n";
  65. }
  66.  
  67. //
  68. // entry point for the example program
  69. //
  70. void main(void)
  71. {
  72.     DPMIhost dpmi;
  73.  
  74.     if (dpmi.getStatus() == DPMIok)
  75.     {
  76.         dpmi.enterProtectedMode(16);
  77.         TestExceptions();
  78.     }
  79.     else
  80.         cout << "DPMI host not present\n";
  81. }