Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 443
Daniel Avatar asked Mar 11 '10 03:03

Daniel


1 Answers

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.

like image 144
danben Avatar answered Sep 19 '22 14:09

danben