Having problems accessing the fields of a structure...
Let's say we're handed a pointer to (the address of) a data struct...
How can we access its fields?
I've tried
and also

struc IED {
.Characteristics DWORD ?
.TimeDateStamp DWORD ?
.MajorVersion WORD ?
.MinorVersion WORD ?
.nName DWORD ?
.nBase DWORD ?
.NumberOfFunctions DWORD ?
.NumberOfNames DWORD ?
.AddressOfFunctions DWORD ?
.AddressOfNames DWORD ?
.AddressOfNameOrdinals DWORD ?
}

We are returned only a pointer to an IED structure.

virtual at 0
IED IED ?,?,?,?,?,?,?,?,?,?,?
end virtual
throws an error.

Anyone care to shed some light on FASM indirect addressing?
Posted on 2003-05-23 06:23:23 by Homer
IMHO this was discussed many times here, but anyway:

struc IED {

.Characteristics DWORD ?
.TimeDateStamp DWORD ?
.MajorVersion WORD ?
.MinorVersion WORD ?
.nName DWORD ?
.nBase DWORD ?
.NumberOfFunctions DWORD ?
.NumberOfNames DWORD ?
.AddressOfFunctions DWORD ?
.AddressOfNames DWORD ?
.AddressOfNameOrdinals DWORD ?
}

virtual at 0 ; or almost the same: "struct IED"
IED IED
end virtual


... and then:

movzx eax,[edi+IED.MajorVersion]
mov ebx,[edi+IED.AddressOfFunction]
...and so on....


General: "struc" don't define any label. It's only "good wish". The name of the struc is not a label. You must create some real data using "good wish" struc and after this you can use real addresses (labels). If you don't want (or you can't) create real structure, you must create virtual one (virtual data and code dosn't go to output file, they exists only during compilation process) at offset 0 and using it as offsets to your real data. Of course there are some more complex constructions: "virtual at edi" for example, but if you understand the common behaviour of FASM it will be easy to use more complex cases.

Regards
Posted on 2003-05-23 06:40:19 by JohnFound
virtual at 0
IED IED
end virtual

From version 1.46 of FASM: this error report... IED [1]
IED IED
error: illegal instruction.
Posted on 2003-05-23 09:06:42 by Homer
Placing the Virtual statement INSIDE THE STRUCT DEFINITION got rid of that error - however, add ecx, is still illegal.
Posted on 2003-05-23 10:59:40 by Homer
First of all: You can use the struct macro for that purpose, it's in the MACRO subdirectory of standard Win32 includes pack for fasm. Files with EQUATES subdirectory show how this macro should be used.

Second: you use DWORD term in your structure definition, but did you define it? If not, you can simply place DWORD equ dd at the beginning of source to get it working.

Third: There's also another way than using the struct macro, I recommend it, altough nobody seems to use this feature: if you want to access structure pointed to by edi register, you can define labels solely for that purpose:


virtual at edi
foo IED
end virtual

Where foo should be your name for that structure, you can name it for example IED@edi if you want name telling exactly what it is. And then use it just like a standard structure: (or ).

And last but not least: please read carefully sections 2.2 - 2.3 of fasm documentation; I've really put much effort to explain usage of all directives there.
Posted on 2003-05-23 12:05:54 by Tomasz Grysztar
Thanks, I'll re-read the docs, until my eyes melt in their sockets..the "struct" macro was undefined? doesn't "struct" define in the data segment by default?
What I'm after is definitely a virtual method of defining the structure...
I know what the struct looks like, but I am only handed a pointer to it - the alternative (hotfix) is to copy the known struct from the dynamic location into the defined structure memory and then mess with it - that seems redundant, the "virtual" seems to be the way to go, any ideas why I'd be getting this kind of error? I mean all I have done is copy the template of examples I'm seeing posted, and it's not working :( What am I doing differently??
Silly me for thinking that a 32 bit assembler would already have DWORD defined as a default type !!
I've heard great things about this assembler, but I am seeing little support for it - should I be translating my masm object / LL support stuff ??
I'm sounding like a wet rag here and it's not my intention.
Posted on 2003-05-25 11:44:27 by Homer
If you need any example, here's the working snippet:


format PE
entry start

include '%include%\win32a.inc'

DWORD equ dd
WORD equ dw
BYTE equ db

OFFSET equ

struc IED {
.Characteristics DWORD ?
.TimeDateStamp DWORD ?
.MajorVersion WORD ?
.MinorVersion WORD ?
.nName DWORD ?
.nBase DWORD ?
.NumberOfFunctions DWORD ?
.NumberOfNames DWORD ?
.AddressOfFunctions DWORD ?
.AddressOfNames DWORD ?
.AddressOfNameOrdinals DWORD ?
}

virtual at edi
IED@edi IED
end virtual

section '.data' data readable writeable

buffer rb 1000h

section '.text' code readable executable

start:
mov edi,OFFSET buffer
mov eax,[IED@edi.TimeDateStamp]
ret


And this is the same snippet, but using the struct macro (this macro defines also sizeof.IED for you, but it's not used here):



format PE
entry start

include '%include%\win32a.inc'

DWORD equ dd
WORD equ dw
BYTE equ db

OFFSET equ

struc IED {
.Characteristics DWORD ?
.TimeDateStamp DWORD ?
.MajorVersion WORD ?
.MinorVersion WORD ?
.nName DWORD ?
.nBase DWORD ?
.NumberOfFunctions DWORD ?
.NumberOfNames DWORD ?
.AddressOfFunctions DWORD ?
.AddressOfNames DWORD ?
.AddressOfNameOrdinals DWORD ?
}
struct IED

section '.data' data readable writeable

buffer rb 1000h

section '.text' code readable executable

start:
mov edi,OFFSET buffer
mov eax,[edi+IED.TimeDateStamp]
ret
Posted on 2003-05-25 12:22:34 by Tomasz Grysztar
Heya .. wheres this "win32a.inc file" available from ??

I tried the first method - got the same kind of error as before.
IED@edi IED error [1] blah

I feel silly :tongue:
Posted on 2003-05-26 06:26:26 by Homer
EvilHomer2k: in \include\ subdir.
you must download the latest fasmw package from fasm website :)
Posted on 2003-05-26 06:28:22 by TBD