flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Macroinstructions > Conditional include

Author
Thread Post new topic Reply to topic
Jin X



Joined: 06 Mar 2004
Posts: 19

Conditional include

I'm doing something like that:

Code:
if      one
    include     'one.inc'
else if two
    include     'two.inc'
else if three
    include     'three.inc'
end if

but fasm loads all 3 files!

And this:

Code:
if      one
    x equ aa
else if two | three
    x equ bb
end if

fasm does x equ aa then x equ bb
Why?

How can I load files and define symbols by conditions?


Last edited by Jin X on 31 Jul 2017, 18:50; edited 2 times in total
Post 14 Apr 2017, 19:03
View user's profile Send private message Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 19

I can't even use macros for define values

Code:
macro set1
{
  const fix 1
}

macro set2
{
  const fix 2
}

these macros definitions (without using) set const to 1 then to 2 !
Very strange... Sad
Post 14 Apr 2017, 19:16
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6685
Location: Kraków, Poland

fasm 1 has multiple processing layers and it is important to know what is processed at what stage. INCLUDE is interpreted by the preprocessor, while IF is processed by the assembler. At the time when the assembler sees the source, all the preprocessing has been done already. Similarly FIX is processed even earlier, as an pre-preprocessing task.
Post 14 Apr 2017, 20:10
View user's profile Send private message Visit poster's website Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 19

Ok, but how can I include different files (or define "equ"s) depending on some conditions?

I need to include a file depending on const that gets value in "virtual" code block (where I check a code size: 32 or 64-bit).

And one more question: why this generates an error?

Code:
a equ hello

macro hello {
  mov eax,ebx
}

a

but "fix" works well...
But I can't use "fix" because I need to select right value depending on some conditions too...
Post 14 Apr 2017, 20:42
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6685
Location: Kraków, Poland


Jin X wrote:
I need to include a file depending on const that gets value in "virtual" code block (where I check a code size: 32 or 64-bit).

It is not possible in fasm 1 architecture, because a value that only becomes known in the assembly stage cannot affect preprocessing stage. It is possible with fasmg engine, though.


Jin X wrote:
And one more question: why this generates an error?

Code:
a equ hello

macro hello {
  mov eax,ebx
}

a

but "fix" works well...
But I can't use "fix" because I need to select right value depending on some conditions too...

The EQU has only an effect for the lines that get passed to the assembler, while macros need to be recognized and processed by the preprocessor. But it possible to extract the value of such variable into a parameter of MATCH macro:

Code:
a equ hello

macro hello { 
  mov eax,ebx 


match valuea {
   value
}

The "value" parameter contains the extracted value of "a" so it gets replaced by "hello" before preprocessing the line in MATCH.
Post 14 Apr 2017, 21:04
View user's profile Send private message Visit poster's website Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 19

Maybe it's worth to do some conditional preprocessor directives that will branch depending on format (pe/pe64/elf32/elf64), bits (16/32/64), symbols using (like "if used") or definitions (like "if defined") and allow to choose "include" file, make "equ"s (maybe "fix" too) and so on? Smile
Post 15 Apr 2017, 08:11
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6685
Location: Kraków, Poland

Preprocessor doesn't know about output format because FORMAT is assembler's directive. But it is possible to define a "format" macro and use MATCH to detect what format does it set.
Post 15 Apr 2017, 09:05
View user's profile Send private message Visit poster's website Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 19

Ok, but you can realize this feature (define constants for directives and make if directives that works on proprocessor layer - as include) Smile
Is it not possible to include file depending on some constant (equ/fix) value now?
Post 31 Jul 2017, 18:56
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 89
Location: Russian Federation, Sochi

Have a nice to play:

Code:
;¤bits equ ¤32;¤64 ; uncomment and play with them or play with conddef
;¤OS equ ¤Windows;¤Linux;¤MenuetOrKolibri
;¤encoding equ ¤ASCII;¤Unicode
;¤language equ ¤RUS;¤ENG  

macro condinc [cond,flname] { match lft == rgt,cond \{ match rgt,lft \\{ include flname \\} \} }
macro conddef [cond,def] { match =cond,cond \{  cond equ def \} }

conddef ¤bits,¤64,\
        ¤encoding,¤ASCII,\
        ¤OS,¤Windows,\
        ¤language,¤ENG

condinc ¤bits ¤OS ¤encoding = =¤64 =¤Windows =¤ASCII,'win64a.inc',\
        ¤bits ¤OS ¤encoding = =¤64 =¤Windows =¤Unicode,'win64w.inc',\
        ¤bits ¤OS ¤encoding = =¤64 =¤Linux =¤ASCII,'lnx64a.inc',\
        ¤bits ¤OS ¤encoding = =¤64 =¤Linux =¤Unicode,'lnx64w.inc',\
        ¤bits ¤OS ¤encoding = =¤64 =¤MenuetOrKolibri =¤ASCII,'men64a.inc',\
        ¤bits ¤OS ¤encoding = =¤32 =¤Windows =¤ASCII,'win32a.inc',\
        ¤bits ¤OS ¤encoding = =¤32 =¤Windows =¤Unicode,'win32w.inc',\
        ¤bits ¤OS ¤encoding = =¤32 =¤Linux =¤ASCII,'lnx32a.inc',\
        ¤bits ¤OS ¤encoding = =¤32 =¤Linux =¤Unicode,'lnx32w.inc',\
        ¤bits ¤OS ¤encoding = =¤32 =¤MenuetOrKolibri =¤ASCII,'kol32a.inc',\
        ¤OS ¤encoding = =¤MenuetOrKolibri =¤Unicode,.err ¤OS unsupport ¤encoding,\
        ¤language = =¤RUS,'lang\ru.inc',\
        ¤language = =¤ENG,'lang\en.inc' 



kol32a.inc

Code:
display 'hello from kolibri32 inc',13,10
include 'x32common.inc'



lnx32a.inc

Code:
display 'hello from linux32a inc',13,10
include 'x32common.inc'



lnx32w.inc

Code:
display 'hello from linux32w inc',13,10
include 'x32common.inc'



lnx64a.inc

Code:
display 'hello from linux64a inc',13,10
include 'x64common.inc'



lnx64w.inc

Code:
display 'hello from linux64w inc',13,10
include 'x64common.inc'



men64a.inc

Code:
display 'hello from menuet64 inc',13,10
include 'x64common.inc'



win32a.inc

Code:
display 'hello from win32a inc',13,10
include 'x32common.inc'



win32w.inc

Code:
display 'hello from win32w inc',13,10
include 'x32common.inc'



win64a.inc

Code:
display 'hello from win64a inc',13,10
include 'x64common.inc'



win64w.inc

Code:
display 'hello from win64w inc',13,10
include 'x64common.inc'



x32common.inc

Code:
display 'hello from x32common inc',13,10



x64common.inc

Code:
display 'hello from x64common inc',13,10



You can improve macro: with help of local macro can catch default or error cases (1 error case I already add); or you can add one more parametr and do additional operating

after all you even can wrap all of it to "¤if" macro, because original "if" is reserved word.
and use smthng like

Code:
¤if ¤bits ¤OS ¤encoding = =¤64 =¤Windows =¤ASCII,'win64a.inc'

[/code]

instead of ¤ you can use any symbol you like from 128-255 of your codepage, or postfix "if" with such symbol instead of prefixing it.
Post 31 Jul 2017, 21:35
View user's profile Send private message Send e-mail Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 19

ProMiNick, thanks! Smile
Post 01 Aug 2017, 08:26
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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2017, Tomasz Grysztar.