views:

500

answers:

2

Which CPU information this code is trying to retrieve. This code is part of a larger package. I am not a Python programmer, and I want to convert this code to C#.

from ctypes import c_uint, create_string_buffer, CFUNCTYPE, addressof
CPUID = create_string_buffer("\x53\x31\xc0\x40\x0f\xa2\x5b\xc3")
cpuinfo = CFUNCTYPE(c_uint)(addressof(CPUID))
print cpuinfo()

If you are a Python programmer and knows what this code is doing, it will be a great help for me.

+22  A: 

It executes the following machine code:

push bx
xor ax, ax
inc ax
cpuid
pop bx
retn

Basically it calls CPUID instruction of the CPU in order to get information about the CPU. Since EAX=1 it gets the processor info and feature bits. The result 32-bit integer is then displayed on the screen, see the wikipedia article or this page to decode the result.

EDIT: Since that's what you're looking for, here's an excellent article about invoking CPUID in a .NET/C# environment (sort of, with P/Invoke)

DrJokepu
Damn :) I came to the same conclusion by googling for the hex value. http://www.cs.helsinki.fi/u/vahakang/ineptkey.pyw and http://en.wikipedia.org/wiki/CPUID gave me the clues.
VVS
You may actually prefer to use WMI to get this sort of information rather than attempting to run raw machine language. Windows does the grunt work for you and delivers so much more than just the CPUID.
paxdiablo
Pax: Very true.
DrJokepu
@DrJokepu: the PO is most likely using a 32-bit machine. Also the upper part of EAX is undetermined if you consider the 16-bit version, so EAX = 1 is probably false.
Bastien Léonard
Bastien Léonard: Yeah, I suppose you're right. I don't have a 32-bit disassembler at hand at the moment but it should be fairly trivial.
DrJokepu
+6  A: 

In addition to DrJokepu's answer. The python code is using the ctypes modules do implement the following C code(/hack):

char *CPUID = "\x53\x31\xc0\x40\x0f\xa2\x5b\xc3"; // x86 code
unsigned int (*cpuid)() = (unsigned int (*)()) CPUID; // CPUID points to first instruction in above code; cast it to a function pointer

printf("%u",cpuid()); // calling cpuid() effectively executes the x86 code.

Also note that this only returns the information in EAX and the x86 code should probably have also pushed/popped the values of ECX and EDX to be safe.

mweerden
Yeah I'm not really at home at Python, so I've just guessed the Python part.
DrJokepu