如何在汇编中访问C预处理程序常量? [英] How to access C preprocessor constants in assembly?

查看:122
本文介绍了如何在汇编中访问C预处理程序常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 C .h文件中定义了一个常量:

If I define a constant in my C .h file:

#define constant 1

如何在我的Assembly .s文件中访问它?

How do I access it in my assembly .s file?

推荐答案

如果使用GNU工具链,则默认情况下,gcc将在扩展名为.S的文件(大写的"S")上运行预处理器.因此,您可以在程序集文件中使用所有cpp功能.

If you use the GNU toolchain, gcc will by default run the preprocessor on files with the .S extension (uppercase 'S'). So you can use all cpp features in your assembly file.

有一些警告:

  • 汇编器和预处理器标记输入的方式可能有所不同.
  • 如果您#include头文件,则它们应仅包含预处理程序指令,而不应包含C函数之类的C ++东西.
  • 您不应使用#注释,因为它们会被预处理程序解释.
  • there might be differences in the way the assembler and the preprocessor tokenize the input.
  • If you #include header files, they should only contain preprocessor directives, not C stuff like function prototypes.
  • You shouldn't use # comments, as they would be interpreted by the preprocessor.

示例:

文件定义.h

#define REGPARM 1

文件汇编

#include "definitions.h"

.text
.globl relocate

    .align 16
    .type relocate,@function
relocate:
#if !REGPARM
    movl  4(%esp),%eax
#endif
    subl  %ecx,%ecx
    ...

即使您不使用gcc,也可以使用相同的方法,只要您的汇编程序的语法与C预处理程序具有合理的兼容性(请参见上面的注意事项).大多数C编译器都可以选择仅预处理输入文件(例如gcc中的-E),或者您可以将预处理器作为单独的可执行文件使用.您可能可以在组装之前将这种预处理包括在构建工具中.

Even if you don't use gcc, you might be able to use the same approach, as long as the syntax of your assembler is reasonably compatible with the C preprocessor (see caveats above). Most C compilers have an option to only preprocess the input file (e.g. -E in gcc) or you might have the preprocessor as a separate executable. You can probably include this preprocessing prior to assembly in your build tool.

这篇关于如何在汇编中访问C预处理程序常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆