flat assembler
Message board for the users of flat assembler.

Index > Windows > How can I modify a heap?

Author
Thread Post new topic Reply to topic
d0mek



Joined: 15 Jul 2013
Posts: 6
d0mek
Since yesterday I started learning assembler and using Fasm, so please excuse if my questions are silly Smile

I want to create a heap, read text file into it, append 0 character at the end of heap and display in a popup.

Here's my code:

invoke HeapCreate, HEAP_NO_SERIALIZE, 8, 0
mov [hHeap], eax
;now I have my 8 byte long heap

invoke ReadFile, [hFile], [hHeap], 8, lpBytesRead, 0
;here I read into the heap


mov [hHeap+2],0
;now I want to add 0 on the end of my heap; THIS DOESN'T WORK- HEAP IS NOT ALTERED

invoke MessageBox, NULL, [hHeap], addr MessageBoxCaption, MB_O
;I display my heap- works
Post 15 Jul 2013, 20:06
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1413
Location: Toronto, Canada
AsmGuru62
HeapCreate will return a handle to the heap object.
To allocate memory from that heap object you must call HeapAlloc.
Post 15 Jul 2013, 22:34
View user's profile Send private message Send e-mail Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: behind the arc
Picnic
hi d0mek,

hHeap is a label say at address 0x401000 which holds a 32-bit value (memory pointer)
you destroy the high bits of pointer by storing a 0 dword at address 0x401002

it makes more sense like this

Code:
mov esi, [hHeap]
mov eax, [lpBytesRead]
move byte [esi+eax], 0     
    
Post 15 Jul 2013, 23:07
View user's profile Send private message Reply with quote
d0mek



Joined: 15 Jul 2013
Posts: 6
d0mek
AsmGuru64:
Value 8 in:
invoke HeapCreate, HEAP_NO_SERIALIZE, 8, 0
means- set size to 8 bytes. Plus it works without HeapAlloc.
Post 17 Jul 2013, 00:08
View user's profile Send private message Reply with quote
d0mek



Joined: 15 Jul 2013
Posts: 6
d0mek
Picnic:
Thank you, this works. But I can do this without esi? If hHeap is a "variable" and "esi" is a variable- why do I have to copy from hHeap to esi?
Post 17 Jul 2013, 00:21
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1413
Location: Toronto, Canada
AsmGuru62
MSDN says that you must use HeapAlloc to get blocks from heap created by HeapCreate.
Post 17 Jul 2013, 12:50
View user's profile Send private message Send e-mail Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
d0mek, you simply don't understand what the "heap" actually is. It is not one block of memory, but a special memory object from where you can dynamically allocate one or many memory blocks depending of your needs. The heap has special structure and contains not only memory you use, but also memory that describes what memory is allocated and what memory is free. As long the exact structure of the heap is unknown and can be changed from version to version, the only legal way to allocate memory from the heap is to use heap functions of Windows.

BTW, there is no need to create a heap with HeapCreate. Every Windows application already have one. Get its handle with "GetProcessHeap" and then use it to allocate one or many blocks of memory with HeapAlloc. Don't forget to free these memory blocks when not needed with "HeapFree".
Post 17 Jul 2013, 21:56
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1288
Location: behind the arc
Picnic
d0mek wrote:
If hHeap is a "variable" and "esi" is a variable- why do I have to copy from hHeap to esi?

[hHeap+2] refers at address 0x401000+2
[esi+2] refers at memory block start address+2
Not the same thing, debug your code and trace values to see what's happening.

I too suggest to use HeapAlloc.
Post 17 Jul 2013, 22:41
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Picnic wrote:
I too suggest to use HeapAlloc.
Suggest is a too weak word in this context. You must insist on using it.
Post 18 Jul 2013, 07:57
View user's profile Send private message Reply with quote
d0mek



Joined: 15 Jul 2013
Posts: 6
d0mek
AsmGuru62, JohnFound, Picnic, baldr: Thank you guys! I will use HeapAlloc then.

I mostly code in PHP, Javascript, Delphi and C# and I don't even need pointers there. I just specify a variable has to be passed by reference and don't need to care any more about it.
Post 18 Jul 2013, 13:31
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
d0mek, so it is time to make some real programming then. Smile Try some web programming in assembly. It is a big fun.
Post 18 Jul 2013, 23:04
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
alessandro95



Joined: 24 Mar 2013
Posts: 62
alessandro95
JohnFound wrote:
d0mek, so it is time to make some real programming then. Smile Try some web programming in assembly. It is a big fun.


Web programming in assembly? That does sound interesting, how do you do that? Can you link any docs/forum/site or whatever?
Post 19 Jul 2013, 12:52
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
alessandro95 wrote:
Web programming in assembly? That does sound interesting, how do you do that? Can you link any docs/forum/site or whatever?
Whole site of Fresh IDE is managed by an assembly written CMS - MiniMagAsm. Also my home site uses the same engine.

It is open source. You can read and download the sources in the repository.

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9
Post 19 Jul 2013, 13:46
View user's profile Send private message Visit poster's website ICQ Number 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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.