Writing Your Own Standard __chkstk Routine

Last reviewed: July 17, 1997
Article ID: Q70173
6.00 6.00a 7.00 | 6.00 6.00a | 1.00 1.50
MS-DOS          | OS/2       | WINDOWS
kbprg kbfasttip

The information in this article applies to:

  • The C Run-time (CRT), included with:

        - Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax
        - Microsoft C for OS/2, versions 6.0, and 6.0a
        - Microsoft C/C++ for MS-DOS, version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
    

SUMMARY

It's possible to write your own stack checking routine.

First, you must allocate some space on the stack and set up the STKHQQ data area. In addition, you must create a near and far version for each memory model. This requires two functions, __aNchkstk (near model) and __aFchkstk (far model), which should be included in every library if you want to override the default stack-checking routines. Finally, you must define the STKHQQ and __chkstk symbols in the default version for the memory model you are working with.

MORE INFORMATION

Below is an example of the replacement code for a small model version of the __chkstk routines. Notice that STKHQQ and __chkstk are defined in the near model version. The STACKSLOP constant is 256 in MS-DOS and 512 under OS/2. To assemble the C 6.0 version define the symbol C600 by adding /DC600 assembler option. You can then either replace these modules in the small or compact libraries, or link with the .OBJs and the /NOE LINK option.

Sample Code

;***** __aNchkstk function ***** ; Assembly options needed - MASM 6.0, 6.0a, 6.0b, and 6.1: /Zm /c /Cx

;                           MASM 5.1: /MX

.MODEL SMALL

IFDEF C600

        EXTRN  _end:word
ELSE
        EXTRN __end:word
ENDIF

include cmacros.inc include msdos.inc

.DATA

IFDEF C600

        PUBLIC STKHQQ
        STKHQQ  dw     dataoffset  _end+STACKSLOP
ELSE
        PUBLIC _STKHQQ
        _STKHQQ dw     dataoffset __end+STACKSLOP
ENDIF

.CODE

PUBLIC __chkstk PUBLIC __aNchkstk

__aNchkstk PROC __chkstk:

        pop   cx        ; grab the return address
        sub   sp, ax
        jmp   cx

__aNchkstk ENDP

END

;***** __aFchkstk function ***** ; Assembly options needed - MASM 6.0, 6.0a, 6.0b, and 6.1: /Zm /c /Cx

;                           MASM 5.1: /MX

.MODEL LARGE .DATA

IFDEF C600

        extrn STKHQQ:word
ELSE
        extrn _STKHQQ:word
ENDIF

.CODE _TEXT

PUBLIC __aFchkstk

__aFchkstk PROC

        pop   cx        ; grab the return address
        pop   dx        ; (and its segment)

        sub   sp, ax

        push  dx        ; push the return address
        push  cx
        ret             ; and go back to that address

__aFchkstk ENDP

END


Additional reference words: kbinf 6.00 6.00a 7.00 7.00b 1.00 1.50 chkstk
_chkstk
KBCategory: kbprg kbfasttip
KBSubcategory: CRTIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.