您的位置:首页 > 产品设计 > UI/UE

cpuid function

2015-08-10 16:11 435 查看
先贴上自己糊弄的代码(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里边


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cpuid 汇编 32位