Writing Your Own Standard __chkstk Routine

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:

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

Last Reviewed: July 18, 1997