flat assembler
Message board for the users of flat assembler.

Index > Main > Using the defined operator

Author
Thread Post new topic Reply to topic
PeExecutable



Joined: 26 Jun 2015
Posts: 181
PeExecutable
I have two include files, both of them need the exact same structure. I don't always use both include files at the same time, so I decided to conditionally define both structures in both include files with the if ~defined operator. When I use both include files (which happens rarely) I get an error "Symbol already defined.
Code:
if ~ defined MyStructure
struct MyStructure
 a dd ?
 b dd ?
 c dd ?
 d dd ?
ends
end if
    

I have it in both include files and it gives me an "Already defined error" when assembling.

But if I use this, it works:
Code:
if ~ defined MyStructure.a
struct MyStructure
 a dd ?
 b dd ?
 c dd ?
 d dd ?
ends
end if
    


When I test it against a data element inside that structure, it works. How can I define the same structure in multiple include files, conditionally, without any assembly errors? Very Happy

I guess there is nothing wrong with checking if an element of a structure is defined, but it's an ugly way of doing it, I'm looking for a better way. Cool
Post 25 Jul 2015, 22:03
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
I can not check your example - you didn't use the struc operator (is not named struc<t>) - must be a macro. If so, which one ?

I would refer to the manual, "2.3.4 structures" in detail and in general the section "2.2.2 conditional assembly". There is existing a "used" operator as well as an "eqtype" operator which may be useful in this context. What is working with defined operator is check of constants and labels, there maybe restriction on complex data types. This is different here to C language.

I use labels in my programs to avoid double definitions in conjunction with defined.
Post 26 Jul 2015, 00:17
View user's profile Send private message Send e-mail Reply with quote
PeExecutable



Joined: 26 Jun 2015
Posts: 181
PeExecutable
All include files that come with fasm uses struct.

Code:
if ~ defined include_extra_include_file
  include_extra_include_file equ 1
  include 'equates\extra.inc'
end if
    


That one worked. Exclamation (Everything compiles normally again)


Last edited by PeExecutable on 26 Jul 2015, 10:04; edited 1 time in total
Post 26 Jul 2015, 01:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17717
Location: In your JS exploiting you and your system
revolution
The "if defined" is ignored by the preprocessor and both structures get defined regardless. For conditional preprocessing you will need to use something like match.
Post 26 Jul 2015, 05:06
View user's profile Send private message Visit poster's website Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
PeExecutable wrote:
All include files that come with fasm uses struct.


Maybe - but not every programmer uses FASM's include files. Cool Could have been something own defined, could have been renamed struc to struct with "fix" directive or something other own defined structure. So at least it would be helpful a piece of code which runs itself in FASM - not just a portion of your code.

So struct is a macro and a macro is handled by the preprocessor and for the preprocessor you need conditional preprocessing which is quite different to conditional assembly. It is very useful to go through FASM documentation in a silent hour or so to explore the sometimes hidden features - you could learn much unexpected stuff. Wink

I myself detected yesterday that there is a "used" directive - maybe newly added feature in the last years. I am still using version 1.71.01 while I mostly studied the documentation when using the version 1.69.35 or so.
Post 26 Jul 2015, 11:25
View user's profile Send private message Send e-mail Reply with quote
PeExecutable



Joined: 26 Jun 2015
Posts: 181
PeExecutable
Thanks for the great tips, I appreciate it. (1.71.01 Question Question Laughing )
Post 26 Jul 2015, 23:46
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
PeExecutable wrote:
Thanks for the great tips, I appreciate it. (1.71.01 Question Question Laughing )

I use FASM for 8 bit developments (Z80 version) - so support of newer Intel and AMD instructions, 64bit support and some other directives are not so important for me. But I like the general structure and easyness of FASM. Wink
Post 27 Jul 2015, 12:38
View user's profile Send private message Send e-mail Reply with quote
El Tangas



Joined: 11 Oct 2003
Posts: 120
Location: Sunset Empire
El Tangas
This thread is already a bit cold, but I would like to add my input. People already said this, but let me make it more clear: things like "define", "equ", "macro", "struc", "irps", match", etc. are managed by the preprocessor, they are not affected by "if/end if", this doesn't exist for the preprocessor.
So for the code:
Code:
struc MyStructure {
 a dd ?
 b dd ?
}
if ~ defined MyStructure
struc MyStructure {
 a dd ?
 b dd ?
 c dd ?
 d dd ?
}
end if    


the preprocessor only sees:
Code:
struc MyStructure {
 a dd ?
 b dd ?
}

struc MyStructure {
 a dd ?
 b dd ?
 c dd ?
 d dd ?
}
    


So the structure will be defined and then redefined, no matter the if/end if

Then, comes the assembler, it doesn't see preprocessing stuf, because it has already been preprocessed, defines, equ's, macros, etc replaced by their values. So (I'm not entirely clear on this), the assembler will see:

Code:

if ~ defined MyStructure <- assembler doesn't understand this
end if    


It will not understand "MyStructure" in that position, because structures can't be placed there. "defined" only works on assembly time variables and constants, meaning labels and numeric variables:
Code:
;stuff that "defined" understands:
label const at 1
here:
nothing=0
    


So in this case, "const", "here" and "nothing" would be valid for "defined".
What I do when I need some conditional preprocessing, is, for example:
Code:
define Im_defined anything can be here
irpv values, Im_defined {
        common
        ;stuff to be preprocess only if "Im_defined" exists, for example:
                irps value, values \{
                        display \`value,13,10
                \}
}
    


this code will display:
Code:
anything
can
be
here    


If you comment out the define, nothing will happen.
Post 09 Aug 2015, 02:23
View user's profile Send private message Reply with quote
PeExecutable



Joined: 26 Jun 2015
Posts: 181
PeExecutable
deleted this post, I found something by searching
Post 22 Aug 2015, 22:11
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.