News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Trying to use structures on blocks of memory

Started by Teloboy, November 12, 2010, 07:41:38 PM

Previous topic - Next topic

Teloboy

I have read a data file into memory and I am trying to use a structure definition to make accessing the data easy but I am hitting head against brick wall and getting no where.
here's a snippet.. taken all the error detection out etc...


   invoke GetFileSize, hFile, addr FileSizeHigh
   mov FileSizeLow, eax
   Invoke GlobalAlloc, GMEM_ZEROINIT Or GMEM_FIXED, FileSizeLow
   mov hRawFile,eax
         
   invoke ReadFile, hFile, hRawFile, FileSizeLow, addr tmpBytesRead,0


;then to access the data I am having to copy it into a var defined as the data structure

      mov edx, hRawFile
      add edx, DatHeader.NumberOfSections
      mov tmpHeader.NumberOfSections, eax

      mov edx, hRawFile
      add edx, DatHeader.NumberOfQuestions
      mov tmpHeader.NumberOfQuestions, eax

but what I really want to do is load edx at the beginning of the memory block and then overlay the structure so I could do things like edx.NumberOfQuestions

Also, the data has effectively 'records' of data in the same structure so I would like to access it by loading a register e.g. ebx with the base and then goto the 10th item and overlay my structure.

I have looked at various tuts about Structures and Unions etc., but all these imply you have to define a variable (which defines space) into that structure but I already have the data loaded.

any ideas/pointers are very welcome... head hurts and blood on the wall !!!!

theunknownguy

Quote from: Teloboy on November 12, 2010, 07:41:38 PM
I have read a data file into memory and I am trying to use a structure definition to make accessing the data easy but I am hitting head against brick wall and getting no where.
here's a snippet.. taken all the error detection out etc...


   invoke GetFileSize, hFile, addr FileSizeHigh
   mov FileSizeLow, eax
   Invoke GlobalAlloc, GMEM_ZEROINIT Or GMEM_FIXED, FileSizeLow
   mov hRawFile,eax
         
   invoke ReadFile, hFile, hRawFile, FileSizeLow, addr tmpBytesRead,0



Struct Test
   Hello  Dword ?
EndS
     
        Assume Eax:Ptr Test
invoke GetFileSize, hFile, addr FileSizeHigh
mov FileSizeLow, eax
Invoke GlobalAlloc, GMEM_ZEROINIT Or GMEM_FIXED, FileSizeLow
mov hRawFile,eax
        Mov [Eax].Hello, 1


- Define struct first
- Use "Assume" on the code

If you want a local struct make:

Local MyStruct:TEST (<- Name of the struct)

Mov MyStruct.Hello, 1


Hope ive help you.
         
   

Teloboy

ok, this looks good, however, how would you extend it to multiple 'records' so that in the below example you have the data having say 10 records of Test..

so for the 5th record I sort of need something like

Mov [Eax].Hello, 1

and then

Mov [Eax+SizeOf Test * 5].Hello, 1


If you see what I mean....

Thanks for the speedy response !!!!!

jj2007

Does that make sense?

      mov edx, hRawFile
      add edx, DatHeader.NumberOfSections
      mov tmpHeader.NumberOfSections, eax

      mov edx, hRawFile
      add edx, DatHeader.NumberOfQuestions
      mov tmpHeader.NumberOfQuestions, eax

By the way: You don't need assume. This syntax works, too:

mov eax, [edx.RECT.left]

Teloboy

Thanks jj2007 this is what I have been trying, but it's a tad awkward when you have 35 - 40 records mainly with the add area.

If I need to access the 15th chunk I need to do something like

mov edx, hRawFile
add edx, (15 * sizeof structure)
mov tmpHeader.NumberOfSections, eax

And I can't really get my head round how to do this correctly... it's all getting a tad messy as the structure has 12 elements in it and performing many multiplications just doesn't seem right to me...


I am hoping I am missing the obvious and someone can shed some much needed light !


jj2007

If I need to access the 15th chunk I need to do something like

mov edx, hRawFile
add edx, (15 * sizeof structure)
mov tmpHeader.NumberOfSections, eax


Correct. Here is an example:

mov edx, 15  ; element #15
imul edx, sizeof MyStructure
add edx, hRawFile
mov eax, [edx.MyStructure.MyElement]  ; assuming that element is DWORD


In MasmBasic, you would write instead:
Dim MyArray(100) As MyStructure
...
mov edx, 15
mov eax, MyArray(edx, MyElement)

... but under the hood it has to do some multiplying, too ;-)

Teloboy

jj2007, that looks good. I kind of came up with something similar this morning but not so neat..

   mov edx, [eax+ebx+Section.hSectionName]

I like the MASM Basic for simplicity but unfortunately I do not no the size of the array at design time.

Thanks for your help.

jj2007

Quote from: Teloboy on November 13, 2010, 08:13:07 AM
I like the MASM Basic for simplicity but unfortunately I do not no the size of the array at design time.
Dim uses HeapAlloc, not .data?, so even if you don't know the size in advance, it's no problem:
mov eax, 123
Dim MyArray(eax) As WNDCLASSEX


In any case, for learning assembler you should do it the hard way. I develop MasmBasic for my own use, because it allows me to mix "true" assembler with a simple "inline Basic", and especially the string routines (e.g. Recall: read a text file from disk, and put it in a string array) are handy and blazingly fast. Many of the fast algos that you find in The Laboratory work "under the hood" of MasmBasic ;-)

Teloboy

Thanks....I'll persist.. I am getting there slowly but surely    :bg