flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Including a C header file using macros ?

Author
Thread Post new topic Reply to topic
wht36



Joined: 18 Sep 2005
Posts: 106
wht36 11 Nov 2008, 01:25
Is there a way to include a C header file and then use preprocessor or assembler directives to convert the function prototypes (into some kind of import list) and/or constants? I tried to do it but there is a problem with parsing // comments.

The load and store assembler directives does not work (I think because they only work on already assembled bytes, and // cannot be assembled).

For example the code below gives 'illegal instruction' error.
Code:
virtual at test1
  repeat test2-test1
          load A byte from %-1
                load B byte from %
          if A='/' & B='/'
                    store byte ';' at %-1
             else
                        store byte A at %-1
         end if
      end repeat
end virtual

test1:
include 'test1.c'
test2:    


where test1.c looks like this
Code:
// sample test file
     mov bx,1
    


The 'fix' directive also does not work because it does not accept '//' as a proper thing to patch and throws an illegal instruction error as well.

So is there anyway out of this predicament? Unless I use FASM to generate a cleaned up file from the header file and include that instead?
Post 11 Nov 2008, 01:25
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 11 Nov 2008, 10:04
wht36,

Is your goal is a fasm source that compiles to some other fasm source with constants, structures and some kind of prototypes for functions from given C source? For it's impossible to write self-modifying source in fasm AFAIK.

Pretty challenging, interpret C source with fasm script…

Your problem with // comments may be solved as follows:
Code:
format binary as "c.fasm"
file "test.c"
in_comment = 0; are we in // comment? Not yet.
repeat $ - $$
    load c byte from $$+%-1
    if in_comment
        if c = 10; LF
            in_comment = 0; single-line comment ends here
        end if
    else
        if (c = '/') & ($$+% < $); got slash and it's not last character in file
            load c byte from $$+%; or else we'll got error here
            if c = '/'; got two slashes
                store byte ';' at $$+%-1; replace first slash with semicolon
                in_comment = 1; and set flag to ignore everything up to LF
            end if
        end if
    end if
end repeat
    
Post 11 Nov 2008, 10:04
View user's profile Send private message Reply with quote
wht36



Joined: 18 Sep 2005
Posts: 106
wht36 11 Nov 2008, 15:59
baldr wrote:
wht36,

Is your goal is a fasm source that compiles to some other fasm source with constants, structures and some kind of prototypes for functions from given C source?


Yes, that is my goal. Initially I considered writing a specific EXE program to convert C prototypes into fasm macros (possibly with type checking), but I should try to do it with macros, since I'm trying to learn macros.

Thanks very much for your code! I was struggling with range errors and couldn't quite figure out how to avoid them! I guess the only way to change "//" into ";/" is to load the file as a binary file and to replace with assembler directives. Oh well, I could always use a batch file to run fasm twice to parse the output again. Many thanks!
Post 11 Nov 2008, 15:59
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 11 Nov 2008, 17:08
wht36,

My example didn't use macros. Just assembler directives. It's possible to write such a script to replace "//" with ";" (not just ";/"), but it will be complex and hard to understand. Now imagine that you're trying to handle /* */ comments as well… Hell of a scripting, almost implementing C syntax parser (with regards to C preprocessor also)… Doesn't it undermine the goal?

Standalone program will fit the bill much more accurately.

There was attempts to use C preprocessor for preprocessing fasm sources with #includes of C header files. Unluckily, it will replace RT_CURSOR with something like (LPSTR)((ULONG_PTR)((WORD)(1))), tasty bit for fasm's error handler…
Post 11 Nov 2008, 17:08
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.