News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Run Time Memory Management

Started by cman, May 17, 2005, 06:14:06 PM

Previous topic - Next topic

cman

How does MASM set-up memory for something like this:


node struct ;struct for node
pdata dd ? ;data in node
next dd NULL ;pointer to next item down stack
node ends





in the main data section of the program? I know variables marked "LOCAL" are placed on the system stack , but where are these variables stored? Is there a range of addresses avaliable for use for a user application in Win32 ? Thanks ....

AeroASM

You can declare data in the data section, allocate it at runtime from the stack, or allocate it at runtime from the heap.

LOCAL variables are on the stack, and they are referenced by esp or ebp.

If you just want a chunk of memory from the heap, do this:

invoke GlobalAlloc,GMEM_FIXED,500
mov pMem,eax

You now have access to 500 bytes of memory, starting at address pMem.
Remember to free it at the end:

invoke GlobalFree,pMem

cman

Thanks for the information! I was interested in how the assembler sets up its static memory management . In static memory management every variable is assigned an address when the program is translated. I wondered which addresses in a Windows program are avaliable for such assignment. Does the translator request the memory from Windows when translating? Or is there a fixed address space that is avaliable? Just curious. :bg

AeroASM

For stuff in the data section, there is a section in the EXE called .data which is normally set up to be loaded at 3000h RVA. Since the image base of EXEs is normally 400000h the start of the data section in memory is normally 403000h. The assembler calculates offsets from this value.

for LOCALs, you have to know what REALLY happens.

Suppose you have a proc without the proc statement ie just a label. Suppose it takes two dwords for parameters. Then:


MyProc:

;alloc space for one dword
sub esp,4

The second argument is at address [esp+12]
The first argument is at address [esp+8]
The return address left by call is at [esp+4]
Our LOCAL dword is at [esp]

;rebalance stack
add esp,4
ret 8



Normally ebp is used instead because esp keeps changing when pushing stuff.


MyProc:
push ebp
mov ebp,esp

sub esp,8
;space for two dword locals

Second argument is at [ebp+20]
First argument is at [ebp+16]
Return address is at [ebp+12]
Old ebp is at [ebp+8]
Second local is at [ebp+4]
first local is at [ebp]

;rebalance stack
mov esp,ebp
ret 8



MASM just calculates the offsets relative to ebp and inserts the prologue and epilogue for you.

cman

Thanks for the information and your time! :U

tenkey

In static memory management, the assembler or compiler simply creates a template for the data space, which may or may not be filled with data. Every "direct" reference to the data space has relocation information associated with it.

The linker combines the data spaces from all the modules it receives, and adjusts the relocation information as needed.

It is the loader (part of Windows) that allocates static memory space when you start the program, using information contained in the PE (EXE or DLL) file. The loader also performs the final relocation that fixes all the "compiled" addresses to their runtime values.

If you can guarantee that certain areas are available, you can define a (nondefault) base address and remove the relocation information from the executable file.

Removing relocation information is not recommended for DLLs, as two DLLs from different authors can collide (i.e., attempt to load in the same memory area). The relocation information allows the the loader to adjust addresses without recompiling your DLL.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

thomasantony

Hi Aero,
   You forgot to do pop ebp in the end. But AFAIK when you use

push ebp
mov ebp,esp

; and

mov esp,ebp
pop ebp

the first argument is at [ebp+8], second at [ebp+12] etc. The local variables are at [ebp-4],[ebp-8] etc. I am sure of this. And BTW, I think it is faster to use the leave instruction than 'mov ebp,esp and pop ebp' .

Thomas :U
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free

AeroASM

You are absolutely right. I was kind of panicking because about halfway through typing that reply I realised that I had a rehearsal in 5 minutes.