CPU Identification

Here is some code to check out your cpu identification using CPUID instruction. Try it first.

#include <cstdio>
#include <windows.h>
#include <assert.h>
#include <cstdlib>
#include <string.h>

bool is_intel_cpu()
{
 char cpu_id[13];

_asm
 {
  MOV EAX, 0
  CPUID
  MOV DWORD PTR [cpu_id], EBX
  MOV DWORD PTR [cpu_id+4], EDX
  MOV DWORD PTR [cpu_id+8], ECX
  MOV BYTE PTR [cpu_id+12], '/0'
 }
 if(strcmp(cpu_id, "GenuineIntel") == 0)
  return true;
 else
  return false;
}

bool supports_rdtsc()
{
 DWORD flag;
 _asm
 {
  MOV EAX, 1
  CPUID
  MOV flag, EDX
 }
 if(flag & 0x10)
  return true;
 else
  return false;
}

bool supports_hyper_threading()
{
 unsigned long flag;
 _asm
 {
  MOV EAX,1
  CPUID
  MOV flag, EDX
 }
 if(flag & 0x10000000)
  return true;
 else
  return false;
}

DWORD cpu_info()
{
 DWORD cpu_speed = 0;
 _asm
 {
  MOV EAX, 80860007H
  CPUID
  MOV cpu_speed, EAX
 }
 return cpu_speed;
}

int main()
{
 unsigned _int32 start = 0, end = 0, result = 10;
 int i,j;
 //printf("%d/n",sizeof(char));

 if(is_intel_cpu())
  printf("This cpu is an intel cpu./n");
 else
  printf("This cpu is not an intel cpu./n");

 if(supports_rdtsc())
  printf("This cpu supports rdtsc./n");
 else
  printf("This cpu doesn't support rdtsc./n");

 if(supports_hyper_threading())
  printf("This cpu supports hyper threading./n");
 else
  printf("This cpu doesn't hyper threading./n");

 printf("Frequency of this cpu is: %xh MHz/n",cpu_info());


 return 0;
}

CPUID is a useful instruction when you want to get some specific information about cpu. With different values in EAX, it will generate different results to show cpu features, by storing them in the other registers.

is_intel_cpu() checks whether the vendor of the cpu is Intel.

supports_rdtsc() checks whether the cpu supports the rdtsc instruction, which can read the time-stamp counter to get the cpu cycles elapsed.

supports_hyper_threading() checks whether the cpu supports hyper threading (HT) technology.

cpu_info() returns the current frequency of the cpu in MHz.

 

By the way, there is no "pointer" in assembly language, even if it is embedded in a C or C++ program. The compiler will give you an error when you try to use pointers in assembly code.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章