Search code examples
assemblygnu-assembler

Is it possible to put (GNU) assembler code for different archs into one source file?


I have a short piece of assembler, that I want to keep in a single file for all supported architectures. One way to do that is to use inline assembly in a C file, but since there is no real C code around, I would rather like to use a (preprocessed) assembler file. Like:

    .file   "zsvjmp.S"

# Here comes the description what this code is supposed to do

    .globl  zsvjmp_
    .text

zsvjmp_:
#if defined (__i386__)
    # here comes the i386 specific assembler code
#elif defined (__arm__)
    @ here comes the arm specific assembler code
#endif

I am however not sure whether this works properly; for example comments are started with # in i386 assembler, while on arm they start with @ - so how common comments would be handled? And maybe there are more pitfalls?

Is there a good way to do this?


Solution

  • If you use gcc, and your assembly source file has the suffix .S, gcc will apply the C preprocessor to your assembly file before sending it to the assembler. Thus # can be used for preprocessor directives, because the preprocessor sees it first. The preprocessor also strips C-style comments (/* */ and //), so you can use those regardless of the assembler you are using.