home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / CLIBSRC3.ZIP / SETJMP.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  10.6 KB  |  385 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - setjmp.cas
  3.  *
  4.  * function(s)
  5.  *      setjmp - nonlocal goto
  6.  *      longjmp - performs a nonlocal goto
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*
  10.  *      C/C++ Run Time Library - Version 5.0
  11.  *
  12.  *      Copyright (c) 1987, 1992 by Borland International
  13.  *      All Rights Reserved.
  14.  *
  15.  */
  16.  
  17.  
  18. #pragma inline
  19. #include <asmrules.h>
  20. #include <setjmp.h>
  21.  
  22. #ifdef  __BOSS__
  23. #include <int2f.h>
  24.  
  25. extern unsigned char _protected;
  26.  
  27. /*
  28.  []---------------------------------------------[]
  29.   |             ModifyCStoID                    |
  30.   |---------------------------------------------|
  31.   |           segment -> ID                     |
  32.   |                                             |
  33.  []---------------------------------------------[]
  34. */
  35.  
  36. int     ModifyCStoID(int segment){
  37.  
  38. asm     push    bx
  39. asm     push    cx
  40. asm     push    dx
  41. asm     push    ds
  42. asm     push    es
  43. asm     push    di
  44. asm     push    si
  45. asm     mov     ax, DGROUP
  46. asm     mov     ds, ax
  47. asm     cmp     byte ptr DGROUP:_protected,0
  48. asm     jnz     sameexit
  49. asm     mov     cx, segment
  50. asm     mov     bx, BOSS_ConvertSelectorToSegmentHandle
  51. asm     mov     ax, 0x0FB42
  52. asm     int     0x02F
  53. asm     cmp     dx, STSUCCESS
  54. asm     je      modseg
  55.  
  56. sameexit:
  57. asm     mov     ax, segment
  58. asm     jmp     exit
  59.  
  60. modseg:
  61.  
  62. asm     mov     es, cx
  63. asm     test    word ptr es:[bx].mpb_flags, NSMOVE
  64. asm     jz      sameexit
  65. asm     shr     bx, 1
  66. asm     shr     bx, 1
  67. asm     shr     bx, 1
  68. asm     shr     bx, 1
  69. asm     add     cx, bx
  70. asm     mov     ax, cx
  71.  
  72. exit:
  73.  
  74. asm     pop     si
  75. asm     pop     di
  76. asm     pop     es
  77. asm     pop     ds
  78. asm     pop     dx
  79. asm     pop     cx
  80. asm     pop     bx
  81.         return(_AX);
  82. }
  83.  
  84. /*
  85. ;[]---------------------------------------------[]
  86. ; |             ModifyIDtoCS                    |
  87. ; |---------------------------------------------|
  88. ; |             CX->CX                          |
  89. ; |                                             |
  90. ;[]---------------------------------------------[]
  91. */
  92.  
  93. int     ModifyIDtoCS(int ID){
  94.  
  95.  
  96. asm     push    bx
  97. asm     push    cx
  98. asm     push    dx
  99. asm     push    ds
  100. asm     push    es
  101. asm     push    di
  102. asm     push    si
  103. asm     mov     ax, DGROUP
  104. asm     mov     ds, ax
  105. asm     cmp     byte ptr DGROUP:_protected,0
  106. asm     jz      real
  107. asm     mov     ax, ID
  108. asm     jmp     exit
  109.  
  110. real:
  111.  
  112. asm     mov     cx, ID
  113. asm     mov     bx, BOSS_LocateMoveableSegment
  114. asm     mov     ax, 0x0FB42
  115. asm     int     0x2F
  116. asm     mov     ax, cx
  117.  
  118. exit:
  119.  
  120. asm     pop     si
  121. asm     pop     di
  122. asm     pop     es
  123. asm     pop     ds
  124. asm     pop     dx
  125. asm     pop     cx
  126. asm     pop     bx
  127.  
  128.         return(_AX);
  129.  
  130. }
  131.  
  132. #endif  // __BOSS__
  133.  
  134. /* leaves current FPU stack pointer in a word at SS:bx */
  135. /* preserves all registers but bx, cx */
  136.  
  137. /*-----------------------------------------------------------------------*
  138.  
  139. Name            setjmp - nonlocal goto
  140.  
  141. Usage           #include <setjmp.h>
  142.                 int setjmp(jmp_buf jmpb);
  143.  
  144. Prototype in    setjmp.h
  145.  
  146. Description     Saves   current context information     (register,  stack
  147.                 position, and segment values)  in the jmp_buf structure and
  148.                 then returns to the caller.
  149.  
  150.                 Setjmp returns 0 when called directly. The caller of setjmp
  151.                 may be  "returned to" again  by longjmp, when  the function
  152.                 result will never be zero.
  153.  
  154.                 The jmp_buf contains  the entire context necessary for  a C
  155.                 task, including  all segments and  the complete flag  word.
  156.                 Only  the  AX,BX,CX,DX,ES  registers are lost, but then the
  157.                 caller  of setjmp  does not expect them  to be  preserved
  158.                 through a function call.
  159.  
  160.                 On entry, the stack frame looks like:
  161.  
  162.                 top     old BP
  163.                         DI
  164.                         SI
  165.                 #if defined (__HUGE__)
  166.                         DS
  167.                 #endif
  168.                         IP
  169.                 #if (LPROG)
  170.                         CS
  171.                 #endif
  172.                         jmpb OFFSET
  173.                 #if (LDATA)
  174.                         jmpb SEGMENT
  175.                 #endif
  176.  
  177. Return value    setjmp returns 0 when it is initially called.
  178.  
  179. *------------------------------------------------------------------------*/
  180. int _CType setjmp(jmp_buf jmpb)
  181. {
  182. asm     pushf
  183. asm     pop     bx              /* save flags in bx */
  184.  
  185. asm     mov     dx, es          /* save es */
  186.  
  187. asm     mov     cx, di          /* save di */
  188.  
  189. #if     LDATA
  190. asm     LES     di, jmpb
  191. #else
  192. asm     mov     di, jmpb
  193. asm     push    DS
  194. asm     pop     ES
  195. #endif
  196.  
  197. asm     cld
  198. asm     lea     ax, jmpb
  199.  
  200. #if defined(__PAS__)
  201. #if     LPROG
  202. asm     add     ax,4
  203. #else
  204. asm     inc     ax
  205. asm     inc     ax
  206. #endif
  207. #endif
  208.  
  209. asm     stosw                   /* sp */
  210. asm     mov     ax, SS
  211. asm     stosw                   /* SS */
  212.  
  213.         /* Save the Floating Point Unit */
  214. #ifdef __DEEP_STACK__
  215. #if defined(__MEDIUM__)  ||  defined(__LARGE__) ||  defined(__HUGE__)
  216. asm     push    ds
  217. asm     mov     ax,_EMUSEG
  218. asm     mov     ds,ax
  219. asm     call    dword ptr ds:[0]
  220. asm     pop     ds
  221. #else
  222. asm     push    cs              /* simulate far call */
  223. asm     call    ds:[___fpustate]
  224. asm     mov     ax, SS:[bx]
  225. asm     stosw                   /* FPU */
  226. #endif
  227. #else
  228. asm     xchg    ax,bx
  229. asm     stosw                   /* flags */
  230. #endif
  231.  
  232. #if     LPROG
  233. asm     mov     ax, W1(jmpb-cPtrSize)   /* large code */
  234. #else
  235. asm     mov     ax, CS
  236. #endif
  237.  
  238. #ifdef  __BOSS__
  239. asm     push    ax
  240. asm     call    _ModifyCStoID
  241. asm     add     sp, 2
  242. #endif
  243.  
  244. asm     stosw                   /* CS */
  245. asm     mov     ax, W0(jmpb-cPtrSize)
  246. asm     stosw                   /* IP */
  247.  
  248. asm     mov     ax, [bp]
  249. asm     stosw                   /* BP */
  250. asm     xchg    ax, cx
  251. asm     stosw                   /* DI */
  252. asm     mov     ax, dx
  253. asm     stosw                   /* ES */
  254. asm     xchg    ax, si
  255. asm     stosw                   /* SI */
  256.  
  257. #if defined(__HUGE__)
  258. asm     pop     ax
  259. asm     push    ax              /* caller's DS */
  260. #else
  261. asm     mov     ax, ds
  262. #endif
  263.  
  264. asm     stosw                   /* DS */
  265. asm     mov     es, dx          /* restore ES */
  266.         return 0;
  267. }
  268.  
  269.  
  270. /*-----------------------------------------------------------------------*
  271.  
  272. Name            longjmp - performs a nonlocal goto
  273.  
  274. Usage           #include <setjmp.h>
  275.                 void longjmp(jmp_buf jmpb, int retval);
  276.  
  277. Prototype in    setjmp.h
  278.  
  279. Description     Restores context information (register, stack position, and
  280.                 segment  values)  from  the  jmp_buf  structure, which must
  281.                 previously  have  been  saved  there  by  setjmp,  and then
  282.                 returns to  the original caller  of setjmp with  val as the
  283.                 return value  as if returning  from setjmp. The  difference
  284.                 between  a  setjmp  return  and  a  longjmp  is that setjmp
  285.                 returns zero, longjmp returns "val". Val can never be zero:
  286.                 if zero argument is supplied, 1 is substituted.
  287.  
  288.                 Longjmp never returns to its own caller. If the contents of
  289.                 jmpb  are not  set, or  if  the  context therein  is for  a
  290.                 function which  is not now  active (if it  has finished and
  291.                 returned) then  the result will  generally be to  crash the
  292.                 program.  The safest  styles of  use of  the setjmp/longjmp
  293.                 pair are to effect a  return  upward through several nested
  294.                 procedure layers  to a parent procedure  designed to handle
  295.                 exceptions, or  to place both  setjmp and longjmp  within a
  296.                 single scheduler/event handler lexical block.
  297.  
  298. Return value    longjmp  cannot return  the value  0; if  passed 0  in val,
  299.                 longjmp will return 1.
  300.  
  301. *------------------------------------------------------------------------*/
  302. void _CType longjmp(jmp_buf jmpb, int retval)
  303. {
  304.         /* sneaky way to do if (retval==0) retval=1; */
  305. asm     mov     dx, retval
  306. asm     cmp     dx, 1           /* generates carry if dx=0      */
  307. asm     adc     dx, 0           /* if was 0, now it's 1  */
  308. asm     LDS_    si, jmpb
  309. asm     cld
  310.  
  311.         /* Change context begins with changing stack */
  312. #ifdef  __BOSS__
  313. asm     push    dx
  314. asm     mov     ax, 0xfb42
  315. asm     mov     bx, BOSS_FreezeUnfreezeStack
  316. asm     mov     cx, FREEZE_SP
  317. asm     int     2fh
  318. asm     pop     dx
  319. #endif
  320.  
  321. asm     pushf                   /* save state of interrupt flag */
  322. asm     pop     bx              /*  in bx */
  323. asm     lodsw                   /* sp */
  324. asm     cli
  325. asm     mov     ss, [si]        /* SS */
  326. asm     mov     sp, ax
  327. asm     push    bx              /* restore state of interrupt flag */
  328. asm     popf
  329. asm     lodsw                   /* skip SS */
  330.  
  331.         /* Restore the Floating Point Unit */
  332. #ifdef __DEEP_STACK__
  333. #if defined(__MEDIUM__)  ||  defined(__LARGE__) ||  defined(__HUGE__)
  334. asm     push    ds
  335. asm     mov     ax,_EMUSEG
  336. asm     mov     ds,ax
  337. asm     call    dword ptr ds:[0]
  338. asm     pop     ds
  339. #else
  340. asm     push    cs              /* simulate far call */
  341. asm     call    ds:[___fpustate]
  342. asm     lodsw                   /* FPU */
  343. asm     mov     SS:[bx], ax
  344. #endif
  345. #else
  346. asm     lodsw                   /* flags */
  347. #endif
  348.  
  349.         /* Build the return-link to the caller of setjmp */
  350. asm     push    ax              /* flags */
  351. asm     lodsw                   /* CS */
  352. asm     push    ax
  353. asm     lodsw                   /* IP */
  354. asm     push    ax
  355.  
  356.         /* Restore other working registers */
  357. asm     lodsw                   /* BP */
  358. asm     xchg    bp, ax
  359. asm     lodsw                   /* DI */
  360. asm     xchg    di, ax
  361. asm     lodsw                   /* ES */
  362. asm     mov     es, ax
  363. asm     lodsw                   /* SI */
  364. asm     mov     ds, [si]
  365. asm     xchg    si, ax
  366.  
  367. #ifdef  __BOSS__
  368. asm     push    dx
  369. asm     mov     ax, 0xfb42
  370. asm     mov     bx, BOSS_FreezeUnfreezeStack
  371. asm     mov     cx, UNFREEZE_SP
  372. asm     int     2fh
  373. asm     pop     dx
  374.  
  375. asm     pop     bx              /* IP  CS on stack!! */
  376. asm     call    _ModifyIDtoCS
  377. asm     add     sp, 2
  378. asm     push    ax              /* CS */
  379. asm     push    bx              /* IP */
  380. #endif
  381.  
  382. asm     xchg    ax, dx          /* put result in ax */
  383. asm     iret                    /* return to original caller of setjmp */
  384. }
  385.