Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this line mean in Python?

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.

like image 234
Priyank Bolia Avatar asked Jun 12 '09 10:06

Priyank Bolia


People also ask

What does [:] do in Python?

A shortcut way to copy a list or a string is to use the slice operator [:] . This will make a shallow copy of the original list keeping all object references the same in the copied list.

What does [:: 2 do in Python?

string[::2] reads “default start index, default stop index, step size is two—take every second element”.

What does \r and \n mean in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.

What does 0 :] mean in Python?

It acts as an indicator in the format method that if you want it to be replaced by the first parameter(index zero) of format. Example : print(42+261={0}.format(303)) Here, {0} will be replaced by 303.


1 Answers

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)

like image 178
Tamas Czinege Avatar answered Oct 02 '22 00:10

Tamas Czinege