NO

Author Topic: Stack allocation function based on chkstk  (Read 3980 times)

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Stack allocation function based on chkstk
« on: February 22, 2018, 06:25:05 PM »
_chkstk.asm obtained from \PellesC\lib\crt.lib :

Code: [Select]
.386
.model flat
option casemap:none

PAGE_SIZE equ 4096

.code

__chkstk PROC

    push    ecx
    cmp     eax,PAGE_SIZE
    lea     ecx,[esp+8H]
    jb      last_page

probe_stack:

    sub     ecx,PAGE_SIZE
    sub     eax,PAGE_SIZE
    test    DWORD PTR [ecx],eax
    cmp     eax,PAGE_SIZE
    jae     probe_stack
   
last_page:

    sub     ecx,eax
    mov     eax,esp
    test    DWORD PTR [ecx],eax
    mov     esp,ecx
    mov     ecx,DWORD PTR [eax]
    mov     eax,DWORD PTR [eax+4]
    push    eax
    ret   

__chkstk ENDP

END

With some minor modifications, this function is converted to allocmem allowing stack reservations exceeding 4096 bytes :

Code: [Select]
include     Demo.inc

.data

string  db '%s',13,10
        db 'Address of the string = %Xh',0

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC

LOCAL pMem:DWORD

     invoke allocmem,8200

     mov    pMem,esp

     mov    DWORD PTR [esp],'sihT'
     mov    DWORD PTR [esp+4],' si '
     mov    DWORD PTR [esp+8],'et a'
     mov    DWORD PTR [esp+12],'.ts'

     invoke printf,ADDR string,pMem,esp

     ret

main ENDP


END start
Code it... That's all...

Offline Vortex

  • Member
  • *
  • Posts: 865
    • http://www.vortex.masmcode.com
Re: Stack allocation function based on chkstk
« Reply #1 on: November 04, 2018, 09:54:09 AM »
Here is the 64-bit version of the chkstk function extracted from \PellesC\Lib\crt64.lib
Code it... That's all...