News:

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

Help with Array of Structures

Started by chrios, March 22, 2011, 01:35:15 PM

Previous topic - Next topic

chrios

Hello.  I've only been learning MASM for a couple of days now and I'm having a lot of trouble figuring out how to access the info I've put into an array of the following structure.

SoftwareInfo STRUCT
VersionName db 7
Version DWORD  0
Model DWORD  0
S DWORD  0
SoftwareInfo ends

.data

Software SoftwareInfo <"V1.00",1,1,0>
SoftwareInfo <"V5.00",5,1,0>
SoftwareInfo <"V10.00",10,1,0>


At the moment I'm trying to create a loop that will go through the whole array and add the items to a combo box.  The VersionName is the text that shows in the combo box and for the combo box data I want to create a DWORD that has the left 16 bits equal the Model, the High byte equal the version number and the low byte equal to S.  Ive tried it several different ways but masm always seems to have an error on the lines where I try to pull info out of the array of structs.

Any pointers are appreciated.  Thanks

donkey

A search for "Array" has found several pages of posts related to this question. It has been asked many many many times.
"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

chrios

Actually, I have been searching for a while now and not at just this site.  I've found many pages that are similar but when I try to implement them they don't work.  I'm sure this is because of something I'm doing wrong, I just don't know what it is.

Sorry if I'm being a nuisance.  I'm sure I'll figure it out.

oex

SoftwareInfo STRUCT
   VersionName      db 7
   Version         DWORD  0
   Model         DWORD  0
   S         DWORD  0
SoftwareInfo ends

.data

Software         SoftwareInfo 3   dup(<>)


Move address of Software to esi
lea esi, Software

Move address of Software.Version to esi
lea esi, Software.Version
Move Software.Version to eax
mov eax, [esi]

Move address of Software.Version to esi
lea esi, Software.Version
add esi, SIZE SoftwareInfo
Move address of Software Structure 2 to eax
mov eax, [esi]

A starting point at least....
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

drizz

May the source be with you...
SoftwareInfo STRUCT DWORD; dword here or make VersionName 8 bytes; strictly for nice alignment - it's not an error
VersionName db 7 dup(0); <--------------- you forgot DUP
Version DWORD  0
Model DWORD  0
S DWORD  0
SoftwareInfo ends

.data

Software SoftwareInfo <"V1.00",1,1,0>
SoftwareInfo <"V5.00",5,1,0>
SoftwareInfo <"V10.00",10,1,0>
Software_Count = ($-Software)/sizeof type Software; count of elements in "Software" array 
Software_End equ $ ; for variant 2

.code
mov ebx,0
mov esi,offset Software
assume esi:ptr SoftwareInfo; set esi type information
.repeat
imul edi,ebx,sizeof SoftwareInfo; calculate index
invoke printf,T("VersionName: %s",CRLF,"Version: %u",CRLF,"Model: %u",CRLF,"S: %u",CRLF),
addr [esi+edi].VersionName,
[esi+edi].Version,
[esi+edi].Model,
[esi+edi].S
inc ebx
.until ebx == Software_Count
assume esi:nothing

; direct access to elements:
lea eax,Software[0*SoftwareInfo].VersionName
lea eax,Software[1*SoftwareInfo].VersionName
lea eax,Software[2*SoftwareInfo].VersionName
mov ecx,Software[0*SoftwareInfo].Version
mov ecx,Software[1*SoftwareInfo].Version
mov ecx,Software[2*SoftwareInfo].Version

; pointer only variant
mov esi,offset Software
assume esi:ptr SoftwareInfo; set esi type information
.repeat
invoke printf,T("VersionName: %s",CRLF,"Version: %u",CRLF,"Model: %u",CRLF,"S: %u",CRLF),
addr [esi].VersionName,
[esi].Version,
[esi].Model,
[esi].S

add esi,type [esi]; you can't to do this with other assemblers
 
.until esi == Software_End
assume esi:nothing
The truth cannot be learned ... it can only be recognized.

dedndave

drizz's last example may also be done without the ASSUME directives like this...
        ; pointer only variant
        mov esi,offset Software
        .repeat
                invoke printf,T("VersionName: %s",CRLF,"Version: %u",CRLF,"Model: %u",CRLF,"S: %u",CRLF),
                        addr [esi].SoftwareInfo.VersionName,
                        [esi].SoftwareInfo.Version,
                        [esi].SoftwareInfo.Model,
                        [esi].SoftwareInfo.S

                add esi,type SoftwareInfo ; you can't to do this with other assemblers

        .until esi == Software_End

i find it convenient - and you don't have the problems if you forget the ASSUME Nothing   :P

chrios

Wow, thank you guys for the detailed help.  I really appreciate it.
This should be more than enough to get me going again.

Thanks again