c++中的__asm__错误
__asm__ in c++ error
我试图用以下代码读取cpuid信息,但它不起作用。我使用的是Visual Studio 2010:
#include "stdafx.h"
#include <stdio.h>
int main()
{
int a, b;
for (a = 0; a < 5; a++)
{
__asm__("cpuid"
:"=a"(b) // EAX into b (output)
:"0"(a) // a into EAX (input)
:"%ebx","%ecx","%edx"); // clobbered registers
printf("The code %i gives %in", a, b);
}
return 0;
}
上面是这样写的:
'project_scs.exe': Loaded 'C:Usersrares992DocumentsVisual Studio 2010Projectsproject_scsDebugproject_scs.exe', Symbols loaded.
'project_scs.exe': Loaded 'C:WindowsSysWOW64ntdll.dll', Symbols loaded (source information stripped).
'project_scs.exe': Loaded 'C:WindowsSysWOW64kernel32.dll', Symbols loaded (source information stripped).
'project_scs.exe': Loaded 'C:WindowsSysWOW64KernelBase.dll', Symbols loaded (source information stripped).
'project_scs.exe': Loaded 'C:WindowsSysWOW64msvcp100d.dll', Symbols loaded.
'project_scs.exe': Loaded 'C:WindowsSysWOW64msvcr100d.dll', Symbols loaded.
The thread 'Win32 Thread' (0x2190) has exited with code -1073741510 (0xc000013a).
The program '[8828] project_scs.exe: Native' has exited with code -1073741510 (0xc000013a).
有谁能告诉我怎么做才能让它运行吗?由于
主要问题是:您使用了用于GCC的AT&T语法。然而,Visual Studio需要英特尔语法。看看下面的例子,看看里面的链接:
#include <stdio.h>
#include <intrin.h>
#define EAX 0
#define EBX 1
#define ECX 2
#define EDX 3
int main ( void )
{
int exx[4], a;
// http://msdn.microsoft.com/en-us/library/4ks26t93%28v=vs.100%29.aspx
puts ("Using VC inline assembler:");
for (a = 0; a < 5; a++)
{
__asm
{
xor ecx, ecx // needed for a=4
mov eax, a
cpuid
mov exx[EAX], eax
}
printf("The code %i gives %08Xn", a, exx[EAX]);
}
// http://msdn.microsoft.com/en-us/library/vstudio/hskdteyh%28v=vs.100%29.aspx
puts ("Using VC intrinsics:");
for (a = 0; a < 5; a++)
{
__cpuid(exx, a);
printf("The code %i gives %08Xn", a, exx[EAX]);
}
return 0;
}
下面是我使用的。它应该与MSVC、ICC、GCC和Clang一起工作。
static inline void cpuid (int output[4], int functionnumber) {
#if defined (_MSC_VER) || defined (__INTEL_COMPILER) // Microsoft or Intel compiler, intrin.h included
__cpuidex(output, functionnumber, 0); // intrinsic function for CPUID
#elif defined(__GNUC__) || defined(__clang__) // use inline assembly, Gnu/AT&T syntax
int a, b, c, d;
__asm("cpuid" : "=a"(a),"=b"(b),"=c"(c),"=d"(d) : "a"(functionnumber),"c"(0) : );
output[0] = a;
output[1] = b;
output[2] = c;
output[3] = d;
#else // unknown platform. try inline assembly with masm/intel syntax
__asm {
mov eax, functionnumber
xor ecx, ecx
cpuid;
mov esi, output
mov [esi], eax
mov [esi+4], ebx
mov [esi+8], ecx
mov [esi+12], edx
}
#endif
}
相关文章:
- Extern "C"错误在'int'之前'asm'或'__attribute__'
- 尝试用纯 c 编程 arduino,得到以下错误:预期的"="、""、";",'asm'或"打印"之前的'__attribute__'|
- 为什么我的混合(C++,asm)程序给我一个分段错误
- 错误:“命令” /重复使用ASM挥发性之前的预期字符串字母
- 错误:在“:”标记之前应为“=”、“”、“;”、“ASM”或“__attribute__”
- 错误:使用gcc 32位时,"asm"操作数具有不可能的约束
- 错误:在令牌之前'asm'或'__attribute__' ':'错误
- MSVC11 错误中的 x64 位 asm
- 阵列到C++功能(使用 ASM)读取错误
- C++:函数返回静态库中的字符串给出错误:在令牌之前':'预期的"="、""、";"、'asm'或'__attribute__'
- 定义宏错误:'asm'之前的预期主异常
- 由于为 c++ 程序调用 ulldiv.asm 而导致编译器引用错误
- bcc32 + nasm obj 链接错误(致命:"fir.asm":需要在 .def 文件中定义其他段)
- 头文件错误:预期的"="、""、";",'asm'或之前'__attribute__'
- 在 GNU 的C++代码中使用 C 标头。包括内联装配的错误:'asm'中不可能的约束
- memcpy.Asm未发现错误
- C++错误:在"I2Cdev"(类名)之前预期为"="、""、";"、"asm"或"__attribute__"
- 收到错误:将我的 c++ 代码从 Linux 移植到 android 时'asm'中的操作数约束不一致
- AVR-C 错误:在令牌之前'asm' '<'预期"="、""、";"、或'__attribute__'
- 内联asm中cmpxchg的分段错误