The specific problem OP had aside: I found a way to define a macro that works for both syntax versions:
#ifdef _MSC_VER
# define ASM(asm_literal) \
__asm { \
asm_literal \
};
#elif __GNUC__ || __clang__
# define ASM(asm_literal) \
"__asm__(\"" \
#asm_literal \
"\" : : );"
#endif
Unfortunately, because the preprocessor strips newlines before macro expansion, you have to surround each assembly statement with this macro.
float abs(float x) {
ASM( fld dword ptr[x] );
ASM( fabs );
ASM( fstp dword ptr[x] );
return x;
}
But please be aware that GCC and clang use AT&T/UNIX assembly synax but MSVC usees Intel assembly syntax (couldn't find any official source though). But fortunately GCC/clang can be configured to use Intel syntax, too. Either use __asm__(".intel_syntax noprefix");
/ __asm__(".att_syntax prefix");
(be sure to reset the changes as it will affect all assembly generated from that point on, even the one generated by the compiler from the C source). This would leave us with a macro like this:
#ifdef _MSC_VER
# define ASM(asm_literal) \
__asm { \
asm_literal \
};
#elif __GNUC__ || __clang__
# define ASM(asm_literal) \
"__asm__(\".intel_syntax noprefix\");" \
"__asm__(\"" \
#asm_literal \
"\" : : );" \
"__asm__(\".att_syntax prefix\");"
#endif
Or you can also compile with GCC/clang using the -masm=intel
flag, which switches the syntax globally.