9

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.

anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140
Priyank Bolia
  • 14,077
  • 14
  • 61
  • 82

2 Answers2

24

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)

Tamas Czinege
  • 118,853
  • 40
  • 150
  • 176
  • 1
    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 Jun 12 '09 at 11:14
  • 2
    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 Jun 12 '09 at 11:41
  • @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 Jun 14 '09 at 12:07
  • 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. – Tamas Czinege Jun 14 '09 at 14:12
6

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.

Community
  • 1
  • 1
mweerden
  • 13,619
  • 5
  • 32
  • 32