views:

306

answers:

2

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

+4  A: 

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.

danben
Nice information thanks, so does that mean EBP - ESP = the total amount of size of the stack frame?
Daniel
Right, and when a new function call is made then the value of EBP is saved on the stack, the value of ESP is moved into EBP, and the size of the new stack frame is subtracted from ESP.
danben
+1 very helpful answer, thanks!
kolistivra
+3  A: 

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.

Seva Alekseyev
Thanks for the info, but it raises more questions xD! How does windows know where to map the heap and stack in the raw RAM so that it doesn't overlap or conflict with other processes? Especially if its dynamically changing in size?
Daniel
@Daniel: wrt other processes, an OS uses virtual memory. Each process sees its own view of the memory (the virtual address space). And yes, the processor has a mechanism to convert a virtual address to a physical one. See http://en.wikipedia.org/wiki/Virtual_memory
Bahbar