The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Teloboy on November 12, 2010, 07:41:38 PM

Title: Trying to use structures on blocks of memory
Post by: 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


;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 !!!!
Title: Re: Trying to use structures on blocks of memory
Post by: theunknownguy on November 12, 2010, 07:50:30 PM
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.
         
   
Title: Re: Trying to use structures on blocks of memory
Post by: Teloboy on November 12, 2010, 09:55:23 PM
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 !!!!!
Title: Re: Trying to use structures on blocks of memory
Post by: jj2007 on November 12, 2010, 10:55:55 PM
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]
Title: Re: Trying to use structures on blocks of memory
Post by: Teloboy on November 12, 2010, 11:05:09 PM
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 !

Title: Re: Trying to use structures on blocks of memory
Post by: jj2007 on November 13, 2010, 07:43:03 AM
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 ;-)
Title: Re: Trying to use structures on blocks of memory
Post by: Teloboy on November 13, 2010, 08:13:07 AM
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.
Title: Re: Trying to use structures on blocks of memory
Post by: jj2007 on November 13, 2010, 09:23:10 AM
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 ;-)
Title: Re: Trying to use structures on blocks of memory
Post by: Teloboy on November 13, 2010, 03:00:49 PM
Thanks....I'll persist.. I am getting there slowly but surely    :bg