是否可以将不同架构的(GNU)汇编代码放入一个源文件中?

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

我有一小段汇编程序,我想将它保存在一个文件中以供所有支持的体系结构使用。一种方法是在 C 文件中使用内联汇编,但由于周围没有真正的 C 代码,我宁愿使用(预处理的)汇编程序文件。喜欢:

    .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

但是我不确定这是否正常工作;例如,在 i386 汇编程序中,注释以 # 开头,而在 arm 上它们以 @ 开头——那么如何处理常见的注释?也许还有更多的陷阱?

有什么好的方法吗?

如果您使用 gcc,并且您的程序集源文件具有后缀 .S,gcc 将在您的程序集文件发送到汇编器之前对其应用 C 预处理器。因此 # 可用于预处理器指令,因为预处理器首先看到它。预处理器还会去除 C-style 注释(/* *///),因此无论您使用的是什么汇编器,您都可以使用这些注释。