flat assembler
Message board for the users of flat assembler.

Index > Windows > conditional defines question

Author
Thread Post new topic Reply to topic
JohnnyReb



Joined: 25 Jun 2006
Posts: 8
JohnnyReb 12 Aug 2006, 14:06
What I would like to do is define a constant "if and only if" it has not been defined previously. I tried:

define TIMERS 1
IF ~ defined TIMERS
define TIMERS 2
end if

but the value of 2 is used.
Any help would be appreciated.

_________________
JohnnyReb
Win if you can, Lose if you must, but always cheat!
Post 12 Aug 2006, 14:06
View user's profile Send private message Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
UCM 12 Aug 2006, 15:03
This is because "if" is processed AFTER "define" and "equ" (which are processed in the preprocessing stage), so if your original code looked like
Code:
define TIMERS 1
if ~ defined TIMERS
define TIMERS 2
end if
mov eax,TIMERS
    
then it is preprocessed to
Code:
if ~ defined TIMERS
end if
mov eax,2
    
.
To make this work, then you will have to use '=' instead of 'define', because '=' is processed in the assembling stage and is affected by 'if'. However, because FASM does multiple passes, you cannot use this simple code:
Code:
TIMERS = 1
if ~ defined TIMERS
TIMERS = 2
end if
mov eax,TIMERS
    
This would work in this scenario, but if you were to not define TIMERS before the 'if' block, then it would cause an infinite loop and FASM would be unable to generate the code. (See the Design Principles article for more information.) Therefore, the solution is to use this:
Code:
TIMERS = 1
if ~ defined TIMERS | defined TIMERS_DEFINED_HERE
TIMERS = 2
TIMERS_DEFINED_HERE = 1
end if
mov eax,TIMERS
    
so that if you do not define TIMERS before the 'if'-block, it will be defined as 2.
Post 12 Aug 2006, 15:03
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8353
Location: Kraków, Poland
Tomasz Grysztar 12 Aug 2006, 17:45
Also see the FAQ (fourth question), and this article (for a bit longer reading).
Post 12 Aug 2006, 17:45
View user's profile Send private message Visit poster's website 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.