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

I 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 perspective. Thus, all sizes would have to be changed to the correct 64-bit versions for me. Or I could (what I did) compile the code using the - 32 flag for as well as the -melf_i386 flag for ld when linking. I also adapted some of the code and ran Windows under Cygwin.

But it made me think. Is there a way to make an ifdef , for example, validation in assembler, to do one if I am on Windows and the other on Linux , and also processes 32 against 64 . For example, for .globl_start on Linux and .globl _main on Windows.

Or is it handled by pre-assembly validation and using different source files to build based on the validation results?

those. foo_linux.s and foo_windows.s

If so, how do you overcome the fact that you don’t know which .s files you will use and therefore should include when you create your program?

For example, let's say that we have socket_linux.s and socket_windows.s . Both of them represent an identical interface, but perform OS-specific work related to sockets. But when I work with sockets in my program, I won’t know if I need a version of Windows or Linux. So I would be screwed a little :)

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 code in #ifdef statements .

+3
source share
3 answers

GCC .S ( S) .sx, .

:

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

-v , , .

+5

MASM (.asm), ifdef, ifndef , :

ifdef X64
endif
+2

:

target.h

#if defined(__arm__)
    #define target "arm"
#elif defined(__x86_64__)
    #if defined(_WIN64)
        #define target "win64"
    #else
        #define target "linux64"  // all non-Win share the same calling convention
    #endif
#else
    // 32bit defs
#endif

, :

#include "target.h"

#include "target_specific_code_" target ".h"

:

target_specific_code_arm.h
target_specific_code_win64.h
target_specific_code_linux64.h
...

EDIT:

:

#ifdef ...
    #define ASM_PP_LOAD_WORD "movi "
#else
    #define ASM_PP_LOAD_WORD "mov "
#endif

#ifdef ...
    // when using intel assembler there is a different
    // order of parameters
    #define ASM_PP_LOAD_WORD(a, b) "movi " #b ", " #a
#else
    #define ASM_PP_LOAD_WORD(a, b) "mov " #a ", " #b
#endif
+1
source

All Articles