Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use #ifdef like checks in assembler?

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.

like image 214
inquam Avatar asked Jun 15 '11 14:06

inquam


1 Answers

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:

file.s
   Assembler code. 
file.S
file.sx
   Assembler code which must be preprocessed.

You can add -v to the command line to see how the various sub-processes are invoked.

like image 139
Igor Skochinsky Avatar answered Sep 28 '22 01:09

Igor Skochinsky