I am new at this.

I am trying to set aside some memory using GlobalAlloc.

My code looks like this:



00401000 >/$ 6A 64          PUSH 64                                  ; /MemSize = 64 (100.)
00401002  |. 6A 00          PUSH 0                                  ; |Flags = GMEM_FIXED
00401004  |. E8 19000000    CALL <JMP.&kernel32.GlobalAlloc>        ; \GlobalAlloc



Ok so far so good. Now I have a handle to my new memory address.

THE PROBLEM
Let's say I move a piece of data, for example 0A to my new memory address using:



MOV DWORD PTR DS:,0A



Now, my question is this: How do I know the next memory address available to use? Because I need to use some more of this memory for other stuff.

THANKS!

Trope
Posted on 2005-04-01 22:23:46 by TropE
you can do

mov byte ptr, 0Ah
mov byte ptr, 0Ah
mov byte ptr, 0Ah
mov byte ptr, 0Ah
mov byte ptr, 0Ah
...
Posted on 2005-04-01 22:36:57 by roticv
After the call to GlobalAlloc, you should store the returned value (EAX) somewhere, so you can dealloc it after use. Also, consider using HeapAlloc instead of GlobalAlloc, as Note  The global functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions. However, the global functions are still used with DDE, the clipboard functions, and OLE data objects. (quote from MSDN/PlatformSDK).
Posted on 2005-04-02 08:11:30 by f0dder
Here's another trick you can use if you need to allocate memory for several arrays but don't want to call the Alloc and Dealloc functions umpteen times. For example, if you need 4 separate arrays of 1000 bytes each:

.data?
Array1 dd ?? ?;address of array1
Array2 dd ?? ?;address of array2
Array3 dd ?? ?;address of array3
Array4 dd ?? ?;address of array4

.code
invoke GlobalAlloc, GPTR, 4000
mov? Array1,eax
add? eax,1000
mov? Array2,eax
add? eax,1000
mov? Array3,eax
add? eax,1000
mov? Array4,eax

....

invoke GlobalFree, Array1    ;deallocates the entire 4000-byte memory block


Raymond
Posted on 2005-04-02 11:25:24 by Raymond
you can always organize your stuff in structure

mystuff struct 4
somearrayofdwords dd 10 dup(?)
mybyte db ?
myfloat dt ?
mystuff ends

invoke GlobalAlloc,GPTR,sizeof mystuff
mov esi,eax
assume esi:ptr mystuff
mov al,.mybyte
mov .mybyte,0ah ;; no <byte ptr> :)
mov .somearrayofdwords,edx
assume esi:nothing

putting hardcoded offsets is bad programming IMHO
everything should be equ-ated and struct-ured
Posted on 2005-04-02 16:30:22 by drizz