用汇编
SYSTEM_INFO sys_info;
GetSystemInfo(&sys_info);
if (sys_info.dwProcessorType==PROCESSOR_INTEL_PENTIUM)
{
try
{
_asm
{
_emit 0x0f ; rdtsc
_emit 0x31
}
}
catch (...) // Check to see if the opcode is defined.
{
TRACE0("RDTSC instruction NOT present.\n");
return FALSE;
}
// Check to see if the instruction ticks accesses something that changes.
volatile ULARGE_INTEGER ts1,ts2;
_asm
{
xor eax,eax
_emit 0x0f ; cpuid
_emit 0xa2
_emit 0x0f ; rdtsc
_emit 0x31
mov ts1.HighPart,edx
mov ts1.LowPart,eax
xor eax,eax
_emit 0x0f ; cpuid
_emit 0xa2
_emit 0x0f ; rdtsc
_emit 0x31
mov ts2.HighPart,edx
mov ts2.LowPart,eax
}
// If we return true then there's a very good chance it's a real RDTSC instruction!
if (ts2.HighPart==ts1.HighPart)
{
if (ts2.LowPart>ts1.LowPart)
{
TRACE0("RDTSC instruction probably present.\n");
return TRUE;
}
else
{
TRACE0("RDTSC instruction NOT present.\n");
return FALSE;
}
}
else if (ts2.HighPart>ts1.HighPart)
{
TRACE0("RDTSC instruction probably present.\n");
return TRUE;
}
else
{
TRACE0("RDTSC instruction NOT present.\n");
return FALSE;
}
}
else
{
TRACE0("RDTSC instruction NOT present.\n");
return FALSE;
}