home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / ERRHNDLR.ZIP / CE.ASM next >
Assembly Source File  |  1989-02-02  |  7KB  |  246 lines

  1. {bt
  2. ;---------------------------------------------------------------------
  3. ; CE.ASM - Critical Error Handler
  4. ;
  5. ;       To assemble:    masm /ml ce;
  6. ;
  7. ; externally accessible functions:
  8. ;
  9. ;       getCEasker - get address of current asker function
  10. ;       setCEasker - set new asker function
  11. ;       CE_trap - interrupt 24h handler
  12. ;---------------------------------------------------------------------
  13.  
  14. _DATA   segment word public 'DATA'
  15. _DATA   ends
  16. DGROUP  group   _DATA
  17.  
  18. _TEXT   segment byte public 'CODE'
  19.         assume cs:_TEXT
  20.  
  21. AskerAddr       dd      0       ; far ptr to current asker fcn
  22. Action          dw      ?       ; 'C', 'R', 'I' or 'A'
  23. ErrorCode       dw      ?       ; error code in DI
  24. CallerAddr      dd      ?
  25. CallerFlags     dw      ?
  26.  
  27. ;---------------------------------------------------------------------
  28. ; setCEasker - change asker function
  29. ;
  30. ; C usage:
  31. ;
  32. ;       void far setCEasker(askerfcnptr fcn);
  33. ;
  34. ; where:
  35. ;       fcn is the address of the new asker
  36. ;---------------------------------------------------------------------
  37.  
  38. public  _setCEasker
  39.  
  40. _setCEasker proc far
  41.  
  42.         push    bp
  43.         mov     bp, sp
  44.  
  45.         mov     ax, [bp+6]
  46.         mov     word ptr cs: AskerAddr, ax
  47.         mov     ax, [bp+8];
  48.         mov     word ptr cs: AskerAddr + 2, ax
  49.  
  50.         pop     bp
  51.         ret
  52.  
  53. _setCEasker endp
  54.  
  55. ;---------------------------------------------------------------------
  56. ; getCEasker - return far ptr to current asker fcn
  57. ;
  58. ; C usage:
  59. ;
  60. ;       askerfcnptr far getCEasker(void)
  61. ;
  62. ;---------------------------------------------------------------------
  63.  
  64. public  _getCEasker
  65.  
  66. _getCEasker     proc far
  67.  
  68.         mov     ax, word ptr cs: AskerAddr
  69.         mov     dx, word ptr cs: AskerAddr + 2
  70.         ret
  71.  
  72. _getCEasker     endp
  73.  
  74. ;---------------------------------------------------------------------
  75. ; CE_trap - Critical Error trap
  76. ;
  77. ;       This function calls the asker function whenever a critical
  78. ;       error occurs.  The asker function must return either 'R' or
  79. ;       'C' in ax.
  80. ;---------------------------------------------------------------------
  81.  
  82. public  _CE_trap
  83.  
  84. _CE_trap        proc far
  85.  
  86.         sti
  87.  
  88.         ; First call the asker function to see what to do
  89.  
  90.         push    ax              ; save entry regs and flags
  91.         push    bx
  92.         push    cx
  93.         push    dx
  94.         push    si
  95.         push    di
  96.         push    bp
  97.         push    ds
  98.         push    es
  99.         pushf
  100.  
  101.         mov     dx, ax          ; set up DS for asker function
  102.         mov     ax, DGROUP
  103.         mov     ds, ax
  104.  
  105.         push    si              ; push C arguments
  106.         push    bp       
  107.         push    di
  108.         push    dx              ; ( pushing previous value of ax)
  109.  
  110.         call    cs: AskerAddr   ; call asker function. On return ax
  111.                                 ;  will be 'C', 'R', 'I' or 'A'
  112.         add     sp, 8           ; remove args from stack
  113.  
  114.         mov     word ptr cs: Action, ax         ; save return val
  115.  
  116.         popf                    ; restore entry state
  117.         pop     es
  118.         pop     ds
  119.         pop     bp
  120.         pop     di
  121.         pop     si
  122.         pop     dx
  123.         pop     cx
  124.         pop     bx
  125.         pop     ax
  126.  
  127.         ; Stack and registers are now in state they were upon entry
  128.         ; and Action contains 'C', 'R', 'I' or 'A' for Cancel, Retry
  129.         ; Ignore or Abort
  130.  
  131.         mov     ax, cs: Action  ; see what asker said to do
  132.         cmp     ax, 'C'
  133.         je      Cancel
  134.         cmp     ax, 'R'
  135.         jne     Ignore
  136.         mov     al, 1           ; Retry operation
  137.         iret
  138.  
  139. Ignore:
  140.         cmp     ax, 'I'
  141.         jne     Abort
  142.         mov     al, 0           ; Ignore Error
  143.         iret
  144.  
  145. Abort:
  146.         mov     al, 2           ; Abort
  147.         iret
  148.  
  149. Cancel: 
  150.                                 ; Cancel operation and go directly to
  151.                                 ;   application that called 21h in
  152.                                 ;   the first place
  153.  
  154.                                 ; convert error code to standard
  155.                                 ; return code and save it
  156.         add     di, 13h
  157.         mov     word ptr cs: ErrorCode, di
  158.  
  159.                                 ; Save caller's return address
  160.         push    bp
  161.         mov     ax, [bp+1ah]
  162.         mov     word ptr cs: CallerAddr, ax
  163.         mov     ax, [bp+1ch]
  164.         mov     word ptr cs: CallerAddr+2, ax
  165.  
  166.                                 ; Set caller's CF and save flags
  167.         mov     ax, [bp+1eh]
  168.         or      ax, 1                   ; set cf in flag image
  169.         mov     word ptr cs: CallerFlags, ax
  170.  
  171.                                 ; Place address of label Stabilize
  172.                                 ;   on stack in place of caller's
  173.                                 ;   return address
  174.         mov     ax, offset cs: Stabilize
  175.         mov     [bp+1ah], ax
  176.         push    cs
  177.         pop     ax
  178.         mov     [bp+1ch], ax
  179.  
  180.         pop     bp
  181.  
  182.         add     sp, 6           ; pop DOS IP, CS and flags
  183.  
  184.         pop     ax              ; restore applications registers
  185.         pop     bx
  186.         pop     cx
  187.         pop     dx
  188.         pop     si
  189.         pop     di
  190.         pop     bp
  191.         pop     ds
  192.         pop     es
  193.  
  194.         iret                    ; transfer to Stabilize
  195.  
  196. Stabilize:
  197.  
  198.         ; When we get here we are officially out of handler and
  199.         ; back into application.
  200.  
  201.         push    bx              ; Save applications registers
  202.         push    cx
  203.         push    dx
  204.         push    si
  205.         push    di
  206.         push    bp
  207.         push    ds
  208.         push    es
  209.  
  210.                                 ; Call harmless DOS function to
  211.                                 ;   stabilize
  212.         mov     ah, 19h         ; Get Current Drive
  213.         int     21h
  214.  
  215.         pop     es              ; Restore applications registers
  216.         pop     ds
  217.         pop     bp
  218.         pop     di
  219.         pop     si
  220.         pop     dx
  221.         pop     cx
  222.         pop     bx
  223.  
  224.         ; Set stack up for IRET to appl
  225.  
  226.                                 ; Push applications flags w/ CF set
  227.         mov     ax, word ptr cs: CallerFlags
  228.         push    ax
  229.                                 ; Push return address
  230.         mov     ax, word ptr cs: CallerAddr+2
  231.         push    ax
  232.         mov     ax, word ptr cs: CallerAddr
  233.         push    ax
  234.                                 ; Put Error Code in ax
  235.         mov     ax, word ptr cs: ErrorCode
  236.  
  237.         iret                    ; Transfer back to application
  238.  
  239. _CE_trap        endp
  240.  
  241. _TEXT   ends
  242.  
  243. END
  244.  
  245. {et
  246.