1

I asked how to dynamically allocate memory in MASM here but I got 2 more questions.

How can I allocate memory for bytes?

.data
tab DB ?
result DB ?

.code
invoke GetProcessHeap
; error here
mov tab, eax          ; I cannot do this because of wrong sizes, AL and AH are equal 0
INVOKE HeapAlloc, tab, 0,  <size>

invoke GetProcessHeap
mov result, eax          ; same here
INVOKE HeapAlloc, result, 0,  <size>

Second question, can I use this method of allocating memory in multithreading application or should I use GlobalAlloc?

Community
  • 1
  • 1
Asker
  • 93
  • 7

1 Answers1

1

HeapAlloc function accepts 3 arguments:

hHeap - handle of a heap object

flags - flags, about how the memory should be allocated

size - The size of the memory block you need

The function returns one double word in EAX that is a pointer to the allocated memory.

You don't need to call GetProcessHeap on every call to HeapAlloc.

The variables tab and result must be double word, because the pointers are double word long (eax)

The memory blocks pointed by these pointers can be accessed in whatever data size you need. They are simply blocks of memory.

Windows heap functions are thread safe and you can use them in multi-thread applications.

How this all will look in assembly:

    .data

tab    dd ?
result dd ?

    .code

    invoke GetProcessHeap
    mov    ebx, eax          ; the heap handle

    invoke HeapAlloc, ebx, 0, <size>
    mov    tab, eax     ; now tab contains the pointer to the first memory block     

    invoke HeapAlloc, ebx, 0,  <size>
    mov    result, eax          ; now result contains the pointer to the second block
johnfound
  • 6,857
  • 4
  • 31
  • 60
  • how can I contact with you? I just want to ask you something about this answer because I get memory violation exception in line: invoke HeapAlloc, result, 0, – Asker Jan 17 '15 at 02:22
  • Writing comment you can contact with me. Also, see my profile. Of course there is a mistake in my answer. That is why you get an exception. :) I will fix it just now. – johnfound Jan 17 '15 at 11:59
  • @Asker Ah, and it is useful to analyze what you read. My mistake was easily detectable from what I write before the source code and the source code itself. – johnfound Jan 17 '15 at 12:10
  • Yes, I noticed that, but it's still causing 'System.AccessViolationException' in the same line: invoke HeapAlloc, ebx, 0, . First HeapAlloc works fine. – Asker Jan 18 '15 at 18:27
  • @Asker HeapAlloc does not change `ebx`, so the exception is because of something else. On my tests this code works fine (of course I changed `` with the real numbers. Did you?) So, execute your code in a debugger step by step and find where is the bug. OllyDbg is a great tool, just for you. – johnfound Jan 18 '15 at 19:27
  • Yes, I changed the size ;) I'm debugging step by step and exception appears after executing this line, when I comment the last 2 lines from example code executes correctly until result array opearation comes ofc. I'm using this code as .dll in my C# application. – Asker Jan 18 '15 at 19:37
  • @Asker - Better write the whole application in assembly. I know MASM is hard, but you can try with (ad!) [FASM/Fresh](http://fresh.flatassembler.net) - it is created for big assembly projects. ;) – johnfound Jan 18 '15 at 22:52
  • I found the cause of error I was using edx instead of ebx ;) Didn't know that I cannot use different register. Thanks for help! – Asker Jan 19 '15 at 00:37
  • Sadly I found another problem, probably the last one, if I create array with known size(tab DD 4 dup (?)) the code executes fine, but when I allocate memory with HeapAlloc (invoke HeapAlloc, ebx, 0, 4) adding something to array changes values of my other variables. – Asker Jan 19 '15 at 01:48
  • @Asker, in your example, `tab` is not the array itself. It is a pointer to the array, allocated with HeapAlloc. – johnfound Jan 19 '15 at 05:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69133/discussion-between-asker-and-johnfound). – Asker Jan 19 '15 at 14:49