cpuid function

先贴上自己糊弄的代码(32位系统下编译):用来在32位系统中读取CPUID指令的各种信息。

cpuid的详细信息请参考64-ia-32-architectures-software-developer-manual-325462,从689页开始

32位下嵌入汇编可以使用_asm{…}格式(汇编语句后面不用加分号)

**************************************************************************************************************************************************

#include<stdio.h>

// 嵌入汇编摘自http://www.cnblogs.com/zyl910/archive/2012/05/21/vcgetcpuid.html

void __cpuidex( int *CPUInfo, int InfoType, int ECXValue ) // 在32位下模拟__cpuidex函数, 64位VS不支持嵌套汇编
   {
        if( NULL == CPUInfo )  return;


        _asm{
            mov edi, CPUInfo    //将数组地址保存在寄存器edi
            mov eax, InfoType   //输入功能号
            mov ecx, ECXValue  //输入子功能号

            cpuid                         //执行cpuid指令

            mov [edi], eax          //返回寄存器的值
            mov [edi+4], ebx
            mov [edi+8], ecx
            mov [edi+12], edx
        }
   }

int main(void) {

    int dwBuf[4] = {0}; // EAX  EBX  ECX  EDX output

    int eax_value; // EAX input/output
    int ecx_value; // ECX input/output
 

    unsigned int flag = 1; // loop control
    unsigned int count; // The count of array
    unsigned int checkSpecial[] = { 0x0D, 0x0F, 0x10, 0x14 }; // special value of EAX

    while (flag){


        printf("\nPlease enter a value as EAX input: ");
        scanf_s("%X", &eax_value);  // assign eax_value
  
        if (((eax_value >= 0x00) && (eax_value <= 0x16)) || ((eax_value >= 0x80000000) && (eax_value <= 0x80000008))) { // Limit of eax_value      


            for (count = 0; count < 4; count++){


                if (eax_value == checkSpecial[count]){ // check special value


                    printf("Please enter a value as ECX input: ");
                    scanf_s("%X", &ecx_value);     //assign ecx_value
                }
            }   


            __cpuidex( dwBuf, eax_value, ecx_value);
   
           printf("EAX: %8X", dwBuf[0]);
           printf("\nEBX: %8X", dwBuf[1]);
           printf("\nECX: %8X", dwBuf[2]);
           printf("\nEDX: %8X", dwBuf[3]);
       }
       else
           printf("\nInput EAX value invalid !!! \n");

           if(eax_value == -1){

                 flag = 0;

           }
   }
   return 0;
}

***********************************************************************************************************************************************************************************************

在64位系统下可以直接调用库函数__cpuidex    参考(https://msdn.microsoft.com/en-us/library/hskdteyh.aspx),

void __cpuidex(
   int cpuInfo[4],
   int function_id,
   int subfunction_id
);
请注意:第一参数int cpuInfo[4] 是指针!是指针!是指针!用于接收eax, ebx, ecx, edx的返回值

function_id即eax的输入值, subfunction_id即ecx的输入值

这个库函数放在头文件intrin.h里边


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值