I started porting some code from MASM to FASM and I have some questions...

MASM has an useful keyword called sizeof... to retrieve the size of a variable or a structure...
This keyword seems to be unavailable in FASM...
Browsing the board, I have seen some tricks to have a sizeof using virtual or other and including it in the structure...
like this :

struc POINT
.x dd ?
.y dd ?
.size = $ - .x

It is a quick fix, but wouldn't it be possible to have a real sizeof, just like MASM or C compilers (and many others) do ?
It would be a more handy and clean imho.
Posted on 2002-08-16 05:41:50 by JCP
This is because fasm is very "low-level" and you can implement all such things your own way by using the preprocessor's abilities.
The most interesting solutions was posted here:
Posted on 2002-08-16 05:48:47 by Tomasz Grysztar
if FASM did macro expansions on included files
I would show you an interesting solution ;)

That was actually the basis for my testing in
that thread. Maybe its something you might
consider doing in a future release?
Posted on 2002-08-16 16:22:24 by Graebel
Fasm does the macro expansion on included files. Or I don't understand what do you mean.

btw: 1.40 beta 8 processes the concatenations before macroinstructions (no compatibility problems, though), so the one of your code samples that didn't work will work with this latest version.

edit: Oh, I see! you had a problem with macros in included, but I'm guessing you had just used macro before it was defined - you included 'user.inc' before 'macros.inc', so when you were using the macro from the second file in the first one, it wasn't defined yet and therefore caused an error.
Posted on 2002-08-16 16:36:02 by Tomasz Grysztar
Hmm, oops... I thought it didnt so I erased that proto-macro from my drive :rolleyes:

What I was originally working on though was something close to this
(non-working example *I think* just to show what I was talking about)

macro struct name
if StartOffset ne <>
sizeof.#StructName = $ - StartOffset
end if
..StructName = name
..StartOffset = $
struct name

Basically it would create the sizeof struct automatically during definition. All but the
last one that is. So if you wanted them all you would place a dummy struct at the
end. If I ever get it working, it would have a couple of gotchas... you would have to
include this before any struct definitions, and also all the structs would have to be
defined back to back with nothing in between or the sizes would be wrong.

Other than that I think this kind of macro would be extreamly helpful.
Posted on 2002-08-16 18:12:29 by Graebel
There is the other problem with that idea: you cannot put definitions of macros inside macros. Why? Because - for example - compiler wouldn't know which macro definition should be ended when the '}' character is met. I could allow this with some small restrictions, I will think more about it, maybe it could make your idea possible. But the totally unstricted is just impossible, because it would lead into ambiguous code.

EDIT: After a bit more thinking I'm sure it's too hard to get that result with my architecture of preprocessor. The macros inside macros shouldn't be allowed unless I want to write another preprocessor from scratch. Maybe we have to invent another solution. What about having some short macro inside the structure definition?
Posted on 2002-08-16 18:26:54 by Tomasz Grysztar
Another strange (but working) solution:

macro ends name
virtual at 0
name name
sizeof.#name = $
end virtual

Now you should define structure like here:

struc POINT
.x dd ?
.y dd ?
ends POINT
Posted on 2002-08-16 18:46:14 by Tomasz Grysztar
Interesting alternative...

I think however, that that defeats the purpose of what I was trying to accomplish.
What I wanted (and looks like its a pipe dream for now), was to have it automated
in some fashion. I like your latest idea, but if you need to add that to the end of
every struct, I do not see where it is any better than the original.

struct POINT
.x dd ?
.y dd ?
.sizeof = $ - .x

Which is ok. I was only testing the waters and seeing if it could be done. Its not
like I actually need all the sizes defined. As I need them I can simply add them in
to the struct itself on demand.

Thanks for the input!
Posted on 2002-08-16 19:35:25 by Graebel
It defines the POINT.x, POINT.y and sizeof.POINT labels, that's why it's more interesting.
Posted on 2002-08-16 19:51:12 by Tomasz Grysztar
Thats nice Privalov, similar to the method I'd been using -
macro DefStruc Strc {

virtual at 0
Strc Strc
end virtual

Which adds "Sze" to the strucs dynamically instead.

I've run into a different sizeof problem however, strings.

Ideally I'd make a DefString macro but unfortunatly at times the parameters I pass to the macro would need to include commas, which won't work.

An example, a macro to do the following is fairly simple

DefString Txt,'Testing'

convert to ->

Txt db 'Testing'
Txt.Sze = $ - Txt

But in parctice a string sometimes needs other characters, if I wanted a string which ended in a new line I'd have to add 13,10 which would be mistaken as macro parameters. eg

DefString Txt,'Testing',13,10

Wouldn't convert to ->

Txt db 'Testing',13,10
Txt.Sze = $ - Txt

In Masm there was a way around this by a VARARG parameter type. Is this possible in Fasm, or perhaps a method of enclosing a parameter in some brackets. You'd know better which solutions are practical.
Posted on 2002-08-16 20:28:23 by Eóin

The exact solution of your problem was already discussed here:
and it is also mentioned in the new docs.
Posted on 2002-08-17 04:30:48 by Tomasz Grysztar
And this is how it can be adapted for your syntax:

macro DefString name,[string]
common name db string
name#.Sze = $ - name
Posted on 2002-08-17 04:37:32 by Tomasz Grysztar
"simply haven't your read it yet?" Sorry, I have'nt read it yet. :(

I'll read it before posting more questions, thanks for the answer. :)
Posted on 2002-08-17 07:04:27 by Eóin