Are there any flags needed to compile code with inlined assembly instructions?
I am trying to get g++ compile the following code (cloned from an answer here on SO):
#include <iostream>
using namespace std;
inline unsigned int get_cpu_feature_flags()
{
unsigned int features;
__asm
{ // <- Line 10
// Save registers
push eax
push ebx
push ecx
push edx
// Get the feature flags (eax=1) from edx
mov eax, 1
cpuid
mov features, edx
// Restore registers
pop edx
pop ecx
pop ebx
pop eax
}
return features;
}
int main() {
// Bit 26 for SSE2 support
static const bool cpu_supports_sse2 = (get_cpu_feature_flags() & 0x04000000)!=0;
cout << (cpu_supports_sse2? "Supports SSE" : "Does NOT support SSE");
}
but I get the following error:
$ g++ t2.cpp
t2.cpp: In function ‘unsigned int get_cpu_feature_flags()’:
t2.cpp:10:5: error: expected ‘(’ before ‘{’ token
t2.cpp:12:9: error: ‘push’ was not declared in this scope
t2.cpp:12:17: error: expected ‘;’ before ‘eax’
$
As others have hinted but not said explicitly, this is incorrect syntax for gcc (which uses a string-based asm(“…”) language instead of true inline assembly code) and for gas (which uses AT&T syntax instead of Intel syntax).
A google for “gcc inline assembly” pulls up this tutorial, which looks good:
http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html
And you can find the relevant section of the gcc documentation here:
http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Extended-Asm.html