flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > macro to support "#define constant value" like in C

Author
Thread Post new topic Reply to topic
alorent



Joined: 05 Dec 2005
Posts: 221
alorent 15 Apr 2017, 11:56
Hi,

I need to create a "constant" file that is shared between a C source code file and a FASM source code file. So, I don't have to deal with 2 different constants file (for C and FASM) and the chances to introduce errors when a value is changed in a file and forgotten to be updated into the other file.

I was wondering if it's possible to create in FASM a macro to support:

Code:
#define my_constant my_value    


and it should be equivalent to:

Code:
my_constant = my_value    


Thanks!
Post 15 Apr 2017, 11:56
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 16 Apr 2017, 18:03
You could write a parser using virtual/load macros (I personally gave up on this very quickly due to the grammar) but I've managed to do this the other way round, where the definitions are in FASM and a separate build stage generates C headers:

definition.fash This is the FASM generator
Code:
macro @define id,val
{
        id equ val
}    

fas2c.fash This is the C generator
Code:
macro @define id,val
{
        db '#define '
        db `id
        db ' '
        db `val
        db NEWLINE
}    

def.fash This is the shared/common definition
Code:
@define CONST,1    

def.h.fasm This will generate a C header
Code:
        format binary as ''

        include 'fas2c.fash'

db NEWLINE
db '#ifndef H_DEF',NEWLINE
db '#define H_DEF,NEWLINE
db NEWLINE
db NEWLINE

        include 'def.fash'

db NEWLINE
db '#endif',NEWLINE    
FASM builds use the def(inition).fash and C builds can use the def.h. (Same trick can be applied to enum/struct/class as well as some meta-programming style stuff)
Post 16 Apr 2017, 18:03
View user's profile Send private message Reply with quote
alorent



Joined: 05 Dec 2005
Posts: 221
alorent 18 Apr 2017, 11:59
Thanks a lot! That's hard stuff for me Smile I will do some tests with it.

Thanks!
Post 18 Apr 2017, 11:59
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.