3

Based on Microsoft MASM Documentation, the usage of .STACK directive is

When used with .MODEL, defines a stack segment (with segment name STACK). The optional size specifies the number of bytes for the stack (default 1,024). The .STACK directive automatically closes the stack statement. (32-bit MASM only.)

For the sake of experimentation, I made the .STACK to allocate 1,073,741,824 bytes (1 GB)

Note that I'm running the code in Visual Studio 2013, console project.

.586

.MODEL FLAT

.STACK 1073741824

.DATA
a DWORD 50
b DWORD 55

.CODE
main PROC
    addLoop: mov eax, a
    push eax
    mov eax, 0
    mov ebx, b
    push ebx
    jmp addLoop
    RET
main ENDP

END

The code will overflow the stack. What I did was I noted down the first address of the ESP register, let the code run until overflowed, and took the final ESP to be subtracted from the first one to get the size of the stack.

In my context, it's 00DAFEE4 - 00CB3000 + 1 = 000FCEE5. Which is only 1036005 bytes (~1 MB).

Why???

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
caramel1995
  • 2,968
  • 10
  • 44
  • 57
  • In 32-bit mode there may not be enough contiguous free address space to have a 1G stack no matter what you do. – Ross Ridge Dec 31 '19 at 21:28
  • 1
    In the 32-bit flat memory model, the stack is created by the OS loader before the program starts running. Its size is a [linker option](https://learn.microsoft.com/en-us/cpp/build/reference/stack-stack-allocations?view=vs-2019), it defaults to [one megabyte](https://stackoverflow.com/a/28658130/17034). – Hans Passant Dec 31 '19 at 21:42
  • What's exactly the use of .STACK??? – caramel1995 Dec 31 '19 at 21:55

1 Answers1

5

Despite what the documentation says, the .STACK directive doesn't do anything useful when creating a 32-bit PECOFF object file. All it does is create an empty section named STACK, regardless of the size given. This directive is meant only to be use used when creating 16-bit code.

Instead using the .STACK directive you can use the the /STACK linker option. You should be able to set this option from the Visual Studio IDE from your project's Property Page -> Linker -> System -> Stack Reserve Size.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112