home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / msc7.zip / C7CKSTK.ASM next >
Assembly Source File  |  1992-02-12  |  4KB  |  188 lines

  1.     page    ,132
  2.     title    chkstk - C stack checking routine
  3. ;***
  4. ;chkstk.asm - C stack checking routine
  5. ;
  6. ;    Copyright (c) 1985-1992 Microsoft Corporation, All Rights Reserved
  7. ;
  8. ;Purpose:
  9. ;    Provides support for automatic stack checking in C procedures
  10. ;    when stack checking is enabled.
  11. ;
  12. ;*******************************************************************************
  13.  
  14. .xlist
  15. ?PLM=0
  16. ?WIN=0
  17.  
  18. ;
  19. ; The way this code is structured, the following switch combinations better
  20. ; not be defined.
  21. ;
  22.  
  23. ;
  24. ; The USE_EXTERN flag indicates whether this module will be the default
  25. ; stack checking module for the library or not.  If so, the stack checking
  26. ; data and subroutines must be defined.  If not, the data and subroutines
  27. ; are NOT defined (i.e., they are externals that reside in the default
  28. ; stack checking object).
  29. ;
  30. ifdef  MI_NEAR
  31.     memS    equ     1            ; near version
  32.     xchkstk equ     <_aNchkstk>
  33.     ifdef  mem_m            ; if the default memory model is
  34.     USE_EXTERNS equ     1        ; medium or large, then use externs
  35.     endif                ; in this module
  36.     ifdef  mem_l
  37.     USE_EXTERNS equ     1
  38.     endif
  39.  
  40. elseifdef MI_FAR
  41.     memM    equ     1            ; far version
  42.     xchkstk equ     <_aFchkstk>
  43.     ifdef  mem_s            ; if the default memory model is
  44.     USE_EXTERNS equ     1        ; small or compact, then use externs
  45.     endif                ; in this module
  46.     ifdef  mem_c
  47.     USE_EXTERNS equ     1
  48.     endif
  49.  
  50. else
  51.     include version.inc         ; default version
  52. endif
  53.  
  54. include cmacros.inc
  55. include msdos.inc
  56.  
  57. .list
  58.  
  59. sBegin    data
  60.     assumes ds,data
  61.  
  62. extrn    __end:word        ; stack bottom
  63.  
  64. ifdef  USE_EXTERNS
  65. ; Use the data that is in the default stack checking module.
  66.  
  67.     if        sizeC
  68.     externCP _aaltstkovr    ; alternate stack overflow
  69.     endif
  70.  
  71.     extrn    _STKHQQ:word
  72.  
  73. else    ;not USE_EXTERNS
  74.  
  75.     globalCP _aaltstkovr,-1 ; alternate stack overflow -- define always so
  76.                 ; mixed model works
  77.  
  78.         public  _STKHQQ    ; used by parasitic heap
  79. ifdef FARSTACK
  80.     ;far stack has bottom at 0
  81.     _STKHQQ  dw      STACKSLOP    ; initial value
  82. else
  83.     _STKHQQ  dw      dataoffset __end+STACKSLOP ; initial value
  84. endif
  85.  
  86. endif    ;not USE_EXTERNS
  87.  
  88. sEnd    data
  89.  
  90.  
  91. sBegin    code
  92. assumes ds,data
  93. assumes cs,code
  94.  
  95. externNP _amsg_exit        ; write error and die
  96.  
  97. page
  98. ;***
  99. ;_chkstk - check stack upon procedure entry
  100. ;
  101. ;Purpose:
  102. ;    Provide stack checking on procedure entry.
  103. ;
  104. ;    [LLIBCDLL.LIB NOTE: Unlike other LLIBCDLL routines, DS != DGROUP
  105. ;    when chkstk() is called.]
  106. ;
  107. ;Entry:
  108. ;    AX = size of local frame
  109. ;
  110. ;Exit:
  111. ;    SP = new stackframe if successful
  112. ;
  113. ;Uses:
  114. ;    AX, BX, CX, DX
  115. ;
  116. ;Exceptions:
  117. ;    Gives out of memory error and aborts if there is not enough
  118. ;    stack space for the routine.
  119. ;
  120. ;*******************************************************************************
  121.  
  122. ifndef USE_EXTERNS
  123. ; Define old label name in default model version
  124. labelP    <PUBLIC,_chkstk>
  125. endif
  126.  
  127. % labelP  <PUBLIC,xchkstk>
  128.  
  129. if    sizeC
  130.     pop    cx        ; get return offset
  131.     pop    dx        ; get return segment
  132. else
  133.     pop    cx        ; get return offset
  134. endif
  135.     mov    bx,sp        ; bp = current SP
  136.  
  137.  
  138.     sub    bx,ax        ; bx = new position
  139. ;*disabled    jc    OMerr        ; error - out of memory
  140.  
  141.  
  142.  
  143. ;*disabled    cmp    bx,[_STKHQQ]    ; SP - AX : _STKHQQ (for heap/stack)
  144. ;*disabled    jb    OMerr        ;   error - out of memory
  145.     mov    sp,bx        ; set new stack pointer
  146.  
  147.  
  148.  
  149.  
  150.  
  151. if    sizeC
  152.     push    dx        ; push segment
  153.     push    cx        ; push offset
  154. chkproc proc    far
  155.     ret            ; far return to dx:cx
  156. chkproc endp
  157. else
  158.     jmp    cx        ; return to cx
  159. endif
  160.  
  161. OMerr:
  162. if    sizeC
  163.     push    dx        ; user segment
  164. endif
  165.     push    cx        ; user offset
  166.  
  167.  
  168. if    sizeC
  169.     mov    ax,word ptr [_aaltstkovr]
  170.     inc    ax
  171.     jnz    altstkovr
  172. endif
  173.  
  174.  
  175.     xor    ax,ax
  176.     jmp    _amsg_exit    ; give stack overflow and die
  177.  
  178.  
  179. if    sizeC
  180. altstkovr:
  181.     jmp    [_aaltstkovr]    ; Pascal/FORTRAN stack overflow
  182. endif
  183.  
  184.  
  185. sEnd    code
  186.  
  187.     end
  188.