是否可以像汇编程序中的检查一样使用#ifdef?
我已经使用 AT&T 语法在 Linux 上测试了一些汇编程序。让我印象深刻的一件事是我正在阅读的这本书是从 32 位的角度编写的。因此,对于我来说,所有大小都必须更改为正确的 64 位版本。或者我可以(我确实这样做了)使用 as 的 --32 标志和 ld 的 -melf_i386 标志来汇编代码 链接时。我还改编了一些代码并在 Windows 上的 Cygwin 下运行。
但这让我开始思考。如果我在 Windows 上,在 Linux 上执行另一件事,并且还处理 < strong>32 与 64 位那样吗?例如,在 Linux 下有一个 .globl _start ,在 Windows 下有一个 .globl _main 。
或者这是通过在组装之前进行检查并根据检查结果来组装不同的源文件来处理的?
即 foo_linux.s 和 foo_windows.s
如果是这样,您如何克服您不知道将使用哪些 .s 文件的事实,因此必须包括,当您创建程序时?
例如,假设我们有一个 socket_linux.s 和一个 socket_windows.s。它们都提供相同的接口,但执行与套接字相关的操作系统特定工作。但是当我在程序中使用套接字时,我不知道是否需要包含 Windows 或 Linux 版本。所以我会有点搞砸了:)
那么这在汇编器中是如何处理的呢?例如,在 C++ 中,我可以包含 socket.h 和 socket.cpp 并将所有 Linux 和 Windows 特定代码包装在 #ifdef 语句中。
I have tested a bit of assembler on Linux using the AT&T syntax. One thing that struck me was that the book I was reading was written from a 32-bit standpoint. Thus, all sizes would have to be changed to the correct 64-bit versions for me. Or I could (which I did) assemble the code using the --32 flag for as and the -melf_i386 flag for ld when linking. I have also adapted some of the code and to run on Windows under Cygwin.
But that got me thinking. Is there a way to do ifdef like checks in assembler to do one thing if I'm on Windows and another under Linux and also handle 32 vs 64 bit that way? For example to have a .globl _start under Linux and a .globl _main under Windows.
Or is this handled by checking before assembling and having different source files to assemble based on the result of the checks?
I.e. foo_linux.s and foo_windows.s
If so, how do you overcome that fact that you will not know which .s files you will use, and thus have to include, when you are creating your program?
For example, say that we have a socket_linux.s and a socket_windows.s. They both present an identical interface but do the OS specific work associated to sockets. But when I work with the sockets in my program I will not know if I need the Windows or Linux version included. So I would be kinda screwed :)
So how is this handled in Assembler? In C++ for example I could include my socket.h and socket.cpp and wrap all the Linux and Windows specific code in #ifdef statements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 GCC 编译文件并将其命名为
.S
(S 大写)或.sx
,它将在调用汇编器之前将它们传递给预处理器。从文档:
您可以添加
-v
到命令行查看各个子流程是如何调用的。If you use GCC to compile your files and name them
.S
(with uppercase S) or.sx
, it will pass them through the preprocessor before invoking the assembler.From the docs:
You can add
-v
to the command line to see how the various sub-processes are invoked.在 MASM (.asm) 中,您可以使用
ifdef
、ifndef
等,如下所示:in MASM (.asm), you can use
ifdef
,ifndef
and the likes, as:当为不同的平台编写时,您可以定义一些宏来加载目标特定文件:
FILE
target.h
然后您可以使用该宏包含目标特定文件,两个字符串文字相继得到一个文字:
它包括一个这些文件的:
编辑:
像这样,您还可以定义目标特定的汇编指令以供稍后在内联汇编中使用:
或作为宏
When writing for different platforms you can define some macro for loading target specific files:
FILE
target.h
Then you can include target specific files with the macro, two string literals successively get one single literal:
It includes one of these files:
EDIT:
Like this, you can also define target specific assembler instructions for later use in inline assembly:
or as macro