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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值