home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / caway349.zip / BIN / _MAYDAY.C next >
C/C++ Source or Header  |  1996-06-17  |  2KB  |  95 lines

  1. // _MAYDAY.C, support module for TESTTERM.C
  2. // 
  3. // This module MUST be compiled with stack-checking turned off (/s option)
  4. //
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. struct InfoListStruct {
  9.     unsigned int ebp;
  10.     unsigned int edi;
  11.     unsigned int esi;
  12.     unsigned int edx;
  13.     unsigned int ecx;
  14.     unsigned int ebx;
  15.     unsigned int eax;
  16.     short unsigned int gs;
  17.     short unsigned int fs;
  18.     short unsigned int es;
  19.     short unsigned int ds;
  20.     unsigned int eip;
  21.     short unsigned int cs;
  22.     short unsigned int reserved1;
  23.     unsigned int eflags;
  24.     unsigned int esp;
  25.     short unsigned int ss;
  26.     short unsigned int reserved2;
  27.     short unsigned int tr;
  28.     unsigned int cr0;
  29.     unsigned int cr1;
  30.     unsigned int cr2;
  31.     unsigned int cr3;
  32.     unsigned int csAddr;
  33.     unsigned int dsAddr;
  34.     unsigned int esAddr;
  35.     unsigned int fsAddr;
  36.     unsigned int gsAddr;
  37.     unsigned int ssAddr;
  38.     short unsigned int Exception;
  39.     unsigned int Code;
  40. };
  41.  
  42. struct InfoListStruct InfoList;
  43.  
  44. extern int _STACKTOP;    // Watcom internal variable, top of Watcom stack
  45.  
  46. // Various contortions necessary to set CauseWay internal stack to
  47. //  Watcom DGROUP-based stack and to keep the unknown number of stack
  48. //  parameters relative to register EBP unchanged.
  49. extern void _ss_to_dgroup(void);
  50. #pragma aux _ss_to_dgroup = \
  51.     "sub ebp,esp"        \
  52.     "xor eax,eax"        \
  53.     "mov ax,ss"            \
  54.     "mov es,ax"            \
  55.     "mov ebx,esp"        \
  56.     "mov edx,_STACKTOP"    \
  57.     "mov cx,ds"            \
  58.     "mov ss,cx"            \
  59.     "mov esp,edx"        \
  60.     "mov edx,esi"        \
  61.     "mov edi,esi"        \
  62.     "sub edi,ebx"        \
  63.     "sub edx,2"            \
  64.     "looper: mov cx,es:[edx]"    \
  65.     "sub esp,2"            \
  66.     "mov ss:[esp],cx"    \
  67.     "sub edx,2"            \
  68.     "sub edi,2"            \
  69.     "jg    looper"            \
  70.     "add ebp,esp"        \
  71.     "push eax"            \
  72.     "push esi"            \
  73.     modify [eax ebx ecx edx edi];
  74.  
  75. extern void _restore_ss_and_go(void);
  76. #pragma aux _restore_ss_and_go =    \
  77.     "pop esi"            \
  78.     "pop eax"            \
  79.     "sub esi,8"            \
  80.     "mov ss,ax"            \
  81.     "mov esp,esi"        \
  82.     "retf";
  83.  
  84. // Termination routine MUST NOT have stack-checking enabled.
  85. // If virtual memory is in use, this routine and all code and data called
  86. //  or used by the routine (including Watcom runtime library functions)
  87. //  must be locked in memory.
  88. void far __loadds Mayday(void)
  89. {
  90.     _ss_to_dgroup();    // lets us call Watcom runtime library functions
  91.     printf("\nAyyahhh, the program is crashing in flames!\n");
  92.     printf("(Incidentally, the exception was %#X and esi was %#X\n)",InfoList.Exception,InfoList.esi);
  93.     _restore_ss_and_go();    // restore stack contorted for Watcom's use
  94. }
  95.