Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

Write a code in C language to obtain CPUID

  1. User

    Write a code in C language to obtain CPUID

  2. Assistant

    As an AI language model, I don't have the capability to write and execute code. However, I can provide you with an example code snippet in C language to obtain CPUID:

    #include <stdio.h>
    #include <stdint.h>
    
    #define CPUID(func,ax,bx,cx,dx)\
        __asm__ __volatile__ ("cpuid":\
        "=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) : "a" (func));
    
    int main()
    {
        uint32_t ax, bx, cx, dx;
        CPUID(0, ax, bx, cx, dx);
        printf("CPU Vendor ID: %s\n", (char *)&ebx);
        return 0;
    }
    

    This code uses the CPUID instruction to retrieve the CPU Vendor ID and prints it to the console. Note that this code is only one example of obtaining CPUID and may not work on all systems.

  3. User

    C language code for obtaining CPUID on Linux

  4. Assistant

    Here's an example code in C language for obtaining CPUID on Linux:

    #include <stdio.h>
    #include <stdint.h>
    #include <cpuid.h>
    
    int main() 
    {
        uint32_t eax, ebx, ecx, edx;
        __cpuid(0, eax, ebx, ecx, edx);
    
        printf("CPU Vendor ID: ");
        printf("%c%c%c%c%c%c%c%c%c\n", 
                ebx & 0xff,
                (ebx >> 8) & 0xff,
                (ebx >> 16) & 0xff,
                (ebx >> 24) & 0xff,
                edx & 0xff,
                (edx >> 8) & 0xff,
                (edx >> 16) & 0xff,
                (edx >> 24) & 0xff,
                ecx & 0xff);
       
        return 0;
    }
    

    This code uses the cpuid.h header file, which provides the __cpuid function. The function takes four parameters: eax, ebx, ecx, and edx. On successful execution, it sets these four variables to store the CPUID values. Here, we're simply retrieving the CPU Vendor ID from the EBX, ECX, and EDX registers and printing it on the console.