【发布时间】:2015-05-03 13:43:19
【问题描述】:
我有一个小班来获取 CpuID 信息。 在类构造函数中,我有一个使用 asm 代码获取 cpuid 信息的内联函数。它在 Windows 中运行良好,在 Xcode 3 中运行良好,但现在类本身被破坏了。
这里是函数的开始:
inline void CCPUDetector::_DetectCPU()
{
char lvendor[13] = "UnknownVendr";
unsigned int lfamily;
unsigned int lmodel;
unsigned int lstepping;
unsigned int lfeatures = 0;
unsigned int lBasicInfo = 0;
uint32_t cpu_cores = 0;
uint32_t cpu_count = 1;
unsigned int signature = 0;
// Step 1: check if a processor supports CPUID instruction
// ============================================================
try
{
__asm
{
// Reset all registers
xor eax, eax
xor ebx, ebx
xor ecx, ecx
xor edx, edx
// Call CPUID with eax register == 0
cpuid
};
}
// Old processors that do not support cpuid will throw exception
// which is caught in the command below "__except"
catch(...)
{
return;
}
}
在 __asm 块 CPUDetector 类本身之后,我在它的构造函数中的那个变得无效(NULL)。我尝试禁用不同的 Xor's 或 cpuid,但每次都得到相同的结果。
有人可以建议我做错了什么吗?
【问题讨论】:
-
你怎么知道是这段代码破坏了班级?
-
您可能应该使用extended asm 并告诉编译器您销毁了寄存器
eax、ebx、ecx和edx。否则,它可能会将其中一个用于重要的事情......请参阅answer with sample code。 -
Ross,我在调试器中看到了。 “this”指针变为NULL,所有成员都是垃圾。
-
小丑,你能指定我怎么知道编译器寄存器被破坏了吗?
标签: xcode assembly xcode5 clang cpuid