.STACK is not allocating the correct size in MASM

≡放荡痞女 提交于 2020-01-24 09:27:05

问题


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???


回答1:


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.



来源:https://stackoverflow.com/questions/59548353/stack-is-not-allocating-the-correct-size-in-masm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!