Windows assembly heap and stack?

OS: Windows 7 32bit

So in like c++ one has a heap and a stack. But i've been starting on some assembly learning lately and haven't seen anything of the sort, only a stack but it just looks like pure memory. So is heap and stack implementation specific for c++ and other languages? Or do you still get allocated a heap and stack in assembly? When starting a executable what does windows do in terms of allocating memory for the process? And how does a process know how big the stack size needs to be?

Whats the go

EDIT: Perhaps someone could provider a link on how heap and stack memory is handled for a process by the CPU/OS


The stack is maintained mostly by the CPU (PUSH/POP/CALL/RET commands); the heap is purely an OS/run-time library feature. Therefore stack access is natural in assembly. For heap access you just call the relevant APIs from your assembly code (HeapAlloc/HeapFree, or from some other library). Unlike stack, there are no low-level primitives in the assembly language for heap memory management.

You don't have to worry about stack size on Windows. As you use up more and more of it, it will grow transparently. In low-level terms, Windows sets up a guard memory page below the stack bottom (assuming stack grows down). When your stack reaches the guard page, an access violation exception is generated in the CPU. Windows kernel would catch it, notice the situation and grow the stack.


Most of my knowledge is not Windows-specific, so bear with me:

The heap and the stack refer to different areas in memory (but we are still talking about main memory in each case). This is not particular to any language. The heap lives in the low memory addresses and grows upwards; the stack lives in the high memory addresses and grows downwards. This is to keep them from overlapping (which would be very bad).

On a 32-bit architecture, the EBP and ESP registers keep track of the current stack frame. EBP is the base pointer - this points to the high address of the current stack frame. ESP is the stack pointer and it points to the low address of the current stack frame.

Remember that the concept of free / allocated heap and stack memory is mostly relevant at the application level. At the machine level, all memory looks the same - it is up to the programmer (or compiler) to keep track of which memory segments are in use.

The stack is managed by a combination of: instructions that call functions, and explicit modifications to EBP and ESP. Anything below ESP is considered freed; so to free memory you can just add to ESP.

The heap is managed by memory allocation methods; documentation can be found here. I am not sure about the particulars of Winows, but in general there will be some memory manager that has the responsibility of making sure no block of memory is allocated to more than one application.

链接地址: http://www.djcxy.com/p/254.html

上一篇: Java堆和堆栈

下一篇: Windows程序集堆和堆栈?