How to move a structure into GlobalAlloc memory?

Started by satpro, March 12, 2012, 02:43:12 PM

Previous topic - Next topic

satpro

Hi All,

I am trying to move a few structures into globally allocated memory during the program run and am freezing on the method/syntax to do this.  I would like to access them as normal structures in the block of memory.

;---------------------------
One of the structures:
;---------------------------
CIA STRUCT
    reg1    DB 0
    reg2    DB 0
    reg3    DB 0

.....  (some more here, reg4-reg14)

    reg15  DB 0
ENDS
;---------------------------


the ptr to the memory:
;---------------------------
pMem  DD (the ptr to some GlobalAlloc memory, lets say 4k in size)


My question,
I have some memory (4k) that needs some structures placed into it.  I would like to use GlobalAlloc to allocate the memory.
I can make a structure in DATA ahead of time manually but I'm wondering how to place these and some other small structures into globally allocated memory once the program is running.  I'm freezing on the method, I think, because the GlobalAlloc mem is in the form of a pointer.  I would like to access the data like regular structures.

Would I be better off making the whole block of memory pre-filled with the structures in an INC file (and not use GlobalAlloc) or can I move the structures into the memory using the pMem pointer?

I hope I'm not too confused or confusing.... 

Thanks
Bert

donkey

Hi satpro,

When you use GlobalAlloc to allocate memory for a structure you have to address them using a register, this is because GoAsm (or any other assembler) cannot know the address of the structure at compile time. So it would look like this:

CIA STRUCT
    reg1    DB 0
    reg2    DB 0
    reg3    DB 0

.....  (some more here, reg4-reg14)

    reg15  DB 0
ENDS

DATA SECTION

pCIA  DD ?

CODE SECTION

// to allocate it
invoke GlobalAlloc, GPTR, SIZEOF CIA
mov [pCIA], EAX

// to access it
mov EDI, [pCIA] // Any general purpose register can be used
mov D[EDI+CIA.reg1], 1234
mov EAX,[EDI+CIA.reg1]


Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

satpro

Donkey,

Ow wow... Thank you.  BTW, that answer was faster than the 7 min. mac & cheese I just made!


I get it.  Thanks again.

Bert