2
votes

I tried to read CPUID using assembler in C++. I know there is function for it in , but I want the asm way. So, after CPUID is executed, it should fill eax,ebx,ecx registers with ASCII coded string. But my problem is, since I can in asm adress only full, or half eax register, how to break that 32 bits into 4 bytes. I used this:

#include <iostream>
#include <stdlib.h>

int main()
{
_asm
{
cpuid
/*There I need to mov values from eax,ebx and ecx to some propriate variables*/
}
system("PAUSE");
return(0);  
}
2
This question as-is could be mis-read as "How can I program in assembly without learning assembly?" based on what you have asked. The obvious answer is for you to learn how to do x86 inline assembly for your preferred environment (Visual C++, GCC on x86 Linux, etc.).mctylr
Why? I actually know assembly better than C. This _asm{} implementation works well in Visual C++. I just dont know how to split these 4bytes into 4 chars using C++.Vit

2 Answers

2
votes

The Linux kernel source shows how to execute x86 cpuid using inline assembly. The syntax is GCC specific; if you're on Windows this probably isn't helpful.

static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
                                unsigned int *ecx, unsigned int *edx)
{
        /* ecx is often an input as well as an output. */
        asm volatile("cpuid"
            : "=a" (*eax),
              "=b" (*ebx),
              "=c" (*ecx),
              "=d" (*edx)
            : "0" (*eax), "2" (*ecx));
}

Once you have a function in this format (note that EAX, ECX are inputs, while all four are outputs), you can easily break out the individual bits/bytes in the caller.

0
votes

I don't understand why you don't use the provided function anyway