views:

131

answers:

2
+1  Q: 

C++ word to bytes

Hi, 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  A: 

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.

Eric Seppanen
A: 

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

stacker
Becouse I love to write simple functions myself :)
Vit
So do I, have you looked up the first link and seen what's in there for you?
stacker
Of course I have. But I am affraid that its too much for me to handle right now. You know, I learned basic asm on 8051 compatible CPUs, so x86 is a chalange. But, could you please tell me one thing? When I write inline asm into C code, with no preprocessor directives, I believe its all set by C code, right? You know, I am new to the game :)
Vit
@stacker, you are a man among typists. ;)
kenny