是否有任何标志需要用内联汇编指令来编译代码?需要编译__asm代码的标志
我试图让G ++编译下面的代码(从答案在这里克隆的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");
}
,但我得到了以下错误:
$ 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’
$
这看起来像VC++可以接受的东西(在Windows上)。您可以在[本维基百科文章](http://en.wikipedia.org/wiki/CPUID)中找到各种其他实现。 –
'__asm'是一个VC++扩展。 C++标准汇编语句看起来像'asm(“code here”);'Gcc使用标准格式,并用一些更多的字符串文字扩展它来声明输入,输出和其他约束。 – bames53