flat assembler
Message board for the users of flat assembler.

Index > Windows > Dynamic Memory Allocation

Author
Thread Post new topic Reply to topic
FlitTear



Joined: 19 Feb 2009
Posts: 6
FlitTear 27 Feb 2009, 08:16
I've googled for about an hour now and i didn't find a way to dynamically allocate a memory in windows.
I've read on some forums that i can use the malloc in c but i can't find the dll that contains the malloc() and free() func.
Please, somebody help me. I'm really stuck on this. Rolling Eyes
Post 27 Feb 2009, 08:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20309
Location: In your JS exploiting you and your system
revolution 27 Feb 2009, 08:34
LocalAlloc, LocalFree
VirtualAlloc, VirtualFree
HeapAlloc, HeapFree.
Post 27 Feb 2009, 08:34
View user's profile Send private message Visit poster's website Reply with quote
FlitTear



Joined: 19 Feb 2009
Posts: 6
FlitTear 27 Feb 2009, 08:42
Oh, thank you very much. I'll try those.
Post 27 Feb 2009, 08:42
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1389
Location: Piraeus, Greece
Picnic 27 Feb 2009, 08:59
Hi FlitTear,
Here is a small example -written on the fly- using VirtualAlloc, maybe it helps.
Code:
        ; Allocate virtual memory

        format PE GUI 4.0
        include 'win32ax.inc'

        MEMSIZE = 256

.data
        MemArea  dd ?
.code
main:
        mov eax, MEMSIZE
        invoke  VirtualAlloc, 0, eax, MEM_COMMIT + MEM_RESERVE, PAGE_READWRITE
        test eax, eax
        je exit

        mov [MemArea], eax

        ; code

        invoke VirtualFree, [MemArea], 0, MEM_RELEASE
exit:
        invoke ExitProcess, 0
.end main
    
Post 27 Feb 2009, 08:59
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20309
Location: In your JS exploiting you and your system
revolution 27 Feb 2009, 09:20
thimis wrote:
Code:
...
        MEMSIZE = 256
...
        mov eax, MEMSIZE
        invoke  VirtualAlloc, 0, eax, MEM_COMMIT + MEM_RESERVE, PAGE_READWRITE
...    
Note the restrictions imposed by the OS
Win32 docs for VirtualAlloc wrote:
dwSize
Specifies the size, in bytes, of the region. If the lpAddress parameter is NULL, this value is rounded up to the next page boundary. Otherwise, the allocated pages include all pages containing one or more bytes in the range from lpAddress to (lpAddress+dwSize). This means that a 2-byte range straddling a page boundary causes both pages to be included in the allocated region.
The page size in Windows is 4kB, so a region size of 256 will be allocated as 4096 bytes minimum.
Post 27 Feb 2009, 09:20
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 789
Location: Adelaide
sinsi 27 Feb 2009, 09:52
For dynamic memory (re)allocation, use the 'Heapxxx' functions - there's even HeapReAlloc (never used the heap functions myself).
Post 27 Feb 2009, 09:52
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1389
Location: Piraeus, Greece
Picnic 27 Feb 2009, 09:53
Thanks for pointing that out revolution! Surprised
Post 27 Feb 2009, 09:53
View user's profile Send private message Visit poster's website Reply with quote
iic2



Joined: 26 Jun 2008
Posts: 122
iic2 27 Feb 2009, 14:18
You'll also find years of study about dynamic memory here. ..

http://f0dder.reteam.org/

click > [ARTICLES]
Post 27 Feb 2009, 14:18
View user's profile Send private message Reply with quote
FlitTear



Joined: 19 Feb 2009
Posts: 6
FlitTear 27 Feb 2009, 16:02
Thanks for all your replies.
I think i'm going to use the VirtulAlloc base on the link given by iic2.
It says their that heapalloc failed on 9x and i might run the prog on 98
Post 27 Feb 2009, 16:02
View user's profile Send private message Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1150
Location: Russian Federation
comrade 28 Feb 2009, 10:34
That is bad advice then.

There is nothing wrong with using HeapAlloc on 9x. If you plan on using VirtualAlloc, you will be committing a full page even though you might only want to allocate ten bytes.
Post 28 Feb 2009, 10:34
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
iic2



Joined: 26 Jun 2008
Posts: 122
iic2 01 Mar 2009, 10:12
You read f0dder site wrong, but at lease you are learning when, where and why. It takes time to catch on to ASM and when you do you'll never turn back.

f0dder recommend HeapAlloc above all else. Fact is, 99% of all he say is correct. He came from the from the stone age with many others around this ASM world. Just be glad that they are willing to share all of that experience with us youngster.

I use HeapAlloc where it's needed but VirtualAlloc proved to work for me and did something special. I been off of ASM for a while. I forgot what ... but it did WIN on my 2-4 k data block ...

They both are API and best of all you are not stuck to using only one API and the cost to use it is still only one dword in size per API to include in any program and the speed is about the same. Here's a test result preformed by another great coder known as MichaelW. Silently search this forum and Google for a few days and learn it all. It took me years of spare time to make up my mind and we all still ask the same question and f0dder will tell us again and again so don't feel shy.


Code:
At start                                           684   168
After 16 calls to GlobalAlloc              1720  1260
After 16 calls to GlobalFree              696   172
After 16 calls to HeapAlloc                824  1260
After 16 calls to HeapFree         696   172
After 16 calls to VirtualAlloc     696  1196
After 16 calls to VirtualFree              696   172
    


BTW: Dump 9x when it comes to today's projects. It's an NT world now. 9x just like DOS is DEAD but old school still love it like me. XP SP2 buck up my entire 9x-XP project with DEP. Who knows what's next in this NT world... 9x is a waste of time other than for a personal tool that rules if you catch the clue. I still love Windows 95
Post 01 Mar 2009, 10:12
View user's profile Send private message Reply with quote
FlitTear



Joined: 19 Feb 2009
Posts: 6
FlitTear 05 Mar 2009, 02:39
Very Happy I'm taking back what i said about using VirtualAlloc.
Actually i can't think of how will i use those extra bytes 'cause i only need 120b. I've thought about link list that can delete a block in between and can also sort the list but i think it will take time for me code it (I'm still new to ASM). Also my deadline is next week. So I used the HeapAlloc for simple management of X numbers of 120b.
Post 05 Mar 2009, 02:39
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.