Thursday, April 16, 2009
CPUID - a simple example
CPUID is an instruction used to query processor specific information. The program below executes cpuid (leaf 0 ) that returns the vendor_id. On linux systems, /proc/cpuinfo will have the vendor_id.
%macro exitprog 0
mov ebx, 0
mov eax, 1
int 0x80
%endmacro
%macro showstring 3
mov eax, 4
mov ebx, %1
mov ecx, %2
mov edx, %3
int 0x80
%endmacro
section .data
here:
times 1 dd 0
eos:
times 1 dd 0
ebx_data dd 0
edx_data dd 0
ecx_data dd 0
segment .text
global _start
_start:
mov eax, 0
cpuid
mov dword [ebx_data], ebx
mov dword [edx_data], edx
mov dword [ecx_data], ecx
showstring 1,ebx_data,12
exitprog
Only the first 2 instructions are important. When cpuid is executed with a value of 0 in eax, it returns the vendor_id information in ebx,edx and ecx. The other instructions in the program merely display the string.
Notice the use of macros. The 'showstring' macro is the file-write system call used in the 'hello world' program. The 'exitprog' is the sys-exit system call.
Upon assembling and running the program the following output is obtained:
GenuineIntel
Please note that depending on the CPU, the output might vary.
On an AMD machine, the following output will be obtained:
AuthenticAMD
(In both cases the result is a 12-byte string contained in ebx,ecx and edx).
%macro exitprog 0
mov ebx, 0
mov eax, 1
int 0x80
%endmacro
%macro showstring 3
mov eax, 4
mov ebx, %1
mov ecx, %2
mov edx, %3
int 0x80
%endmacro
section .data
here:
times 1 dd 0
eos:
times 1 dd 0
ebx_data dd 0
edx_data dd 0
ecx_data dd 0
segment .text
global _start
_start:
mov eax, 0
cpuid
mov dword [ebx_data], ebx
mov dword [edx_data], edx
mov dword [ecx_data], ecx
showstring 1,ebx_data,12
exitprog
Only the first 2 instructions are important. When cpuid is executed with a value of 0 in eax, it returns the vendor_id information in ebx,edx and ecx. The other instructions in the program merely display the string.
Notice the use of macros. The 'showstring' macro is the file-write system call used in the 'hello world' program. The 'exitprog' is the sys-exit system call.
Upon assembling and running the program the following output is obtained:
GenuineIntel
Please note that depending on the CPU, the output might vary.
On an AMD machine, the following output will be obtained:
AuthenticAMD
(In both cases the result is a 12-byte string contained in ebx,ecx and edx).
Subscribe to Posts [Atom]
Post a Comment