News:

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

MACRO: struct declerations and proc args

Started by RedXVII, March 28, 2007, 11:01:40 PM

Previous topic - Next topic

RedXVII

Ok, im stuck again  :red

2 things;

1) I want to cycle through the declerations in a struct, for example if i wanted to cycle through an unkonwn struct checking for a certain value. eg. my macros looking like this,

CCSEARCH MACRO struct
  local decleration
  ;loop - cycle through structs declerations
  mov eax, &struct.&decleration               ;the reason i have &struct and not struct is because im treating it as a string
  cmp eax, 00CC00CCh
  ...

EndM



2) This time I want to get the size of the arguments of a proc. The example may seem strange, but again, this is just a simplified version of a much larger macro and Im trying to make this easy for anyone who can help me  :toothy eg.

ARGSIZEINBYTES MACRO myprocname
  exitm<;get procname arg size here(8) >
EndM

function1 proc x:DWORD, y:DWORD
  ...
  mov eax, ARGSIZEINBYTES(function1)
  ret
function1 endp




Thanks for any help and suggestions offered!   :U



sluggy

Quote from: RedXVII on March 28, 2007, 11:01:40 PM
1) I want to cycle through the declerations in a struct, for example if i wanted to cycle through an unkonwn struct checking for a certain value.
I am puzzled as to why you want to do this. I don't believe there is any way to do this - a struct is simply a bunch of contiguous memory locations, a STRUCT is simply a language construct to help you programamtically access those memory locations in an arbitrary way. IOW there is nothing in memory to say where structs or their fields begin and end. So what i am ultimately saying is that you cannot generically loop over unknown fields in unknown structs - the type of struct has to be known at compile time.

Or have i misunderstood what you were asking?


Tedd

In other languages where this is possible (called RTTI in C++, and Reflection in Java & Python) it works by the compiler storing the information about the size and members of the struct (or class, or whatever else) during compilation and then the functions can use this information at runtime.
So, if you want to simulate this you'll have to store the information explicitly yourself and use that (macros can help you a little, but you'll still have to do a lot of it manually; or get very sophisticated and have macros for defining each struct element, which then adds its size, offset, name, etc, to the table and defines it in the struct -- if this is even possible.)
No snowflake in an avalanche feels responsible.

Timbo

Greetings,

Also take note that structs do not necessarily have to appear in memory as they are declared due to alignment.  Using a compile-time component to get the declared size will work, but getting the runtime layout requires some pointer math.  I think you may be aware of this since you are looking for a predefined value within the struct declaration, but it is unclear (to me anyway) whether you intend to use this during compile or runtime.

There are sources available for garbage collection schemes and alot of this will come in handy for what you may be intending to do.

I would suggest using/writing an external parser that can generate the struct metadata for you (perhaps in db "xxx" format).

Regards,

Tim