One-minute learning to obtain the CPU serial number

Source: Internet
Author: User

I stress again that not all processors have serial numbers! Intel documentation tells us that the Pentium 4 series processor does not support serial numbers! After testing, we found that AMD's machine was not available. It seems that this processor serial number is useless. But after studying it for so long, I recommend it. Maybe someone can use it.

This document tells us that the CPU Sn is a 96-Bit String, which can be obtained by calling the Assembly command cpuid. See the code for details. Only 20 or 30 rows.

Here you can go to the full documentation:

Intel processor identification and the cpuid instruction

The following section describes the processor serial number in the Intel documentation.

A. Presence of processor serial number
To determine if the processor serial number feature is supported, the program shocould set the eax
Register parameter value to "1" and then execute the cpuid instruction as follows:
MoV eax, 01 H
Cpuid
After execution of the cpuid instruction, the ECX and EDX register contains the feature flags.
If the PSN feature flags, (EDX register, bit 18) equals "1", the processor serial number feature is
Supported, and enabled. If the PSN feature flags equals "0", the processor serial number
Feature is either not supported, or disabled.

B. forming the 96-bit processor serial number
The 96-bit processor serial number is the concatenation of three 32-bit entities.
To access the most significant 32-bits of the processor serial number the program shocould set
Eax register parameter value to "1" and then execute the cpuid instruction as follows:
MoV eax, 01 H
Cpuid
After execution of the cpuid instruction, the eax register contains the processor signature.
Processor signature comprises the most significant 32-bits of the processor serial number.
Value in eax shocould be saved prior to gathering the remaining 64-bits of the processor serial
Number.
To access the remaining 64-bits of the processor serial number the program shocould set the eax
Register parameter value to "3" and then execute the cpuid instruction as follows:
MoV eax, 03 h
Cpuid
After execution of the cpuid instruction, The edX register contains the middle 32-bits, and
ECX register contains the least significant 32-bits of the processor serial number. Software may
Then concatenate the saved processor signature, EDX, and ECx before returning the complete 96-
Bit processor serial number.
Processor serial number shocould be displayed as 6 groups of 4 hex nibbles (ex. XXXX-XXXXXXXX-
XXXX-XXXX-XXXX where X represents a hex digit). Alpha hex characters shoshould be
Displayed as capital letters.

The following code obtains the CPU serial number:

Void tohex (const unsigned char * szorigin, int nsize, char * szhex)
{
Char sztemp [10];
For (INT nindex = 0; nindex <nsize; nindex ++)
{
Sprintf (sztemp, "% 02x", szorigin [nindex]);
If (nindex = 0)
{
Strcpy (szhex, sztemp );
}
Else
{
Strcat (szhex, sztemp );
}
}
}

Bool detectcpu ()
{
Char szcpudesc [13];
Memset (szcpudesc, 0, 13 );

Unsigned char szcpusn [12];
Memset (szcpusn, 0, 12 );
 
Unsigned long uleax = 0u, ulebx = 0u, ulecx = 0u, uledx = 0u;
 
_ Try
{
_ ASM
{
MoV eax, 1
Cpuid
MoV uledx, EDX
MoV uleax, eax
}

// Check whether the CPU serial number exists
// Note that the Intel documentation says that the detection of 18th-bit edX is calculated starting from 0th-bit.
If (! (Uledx & (1 <18 )))
Return false;
// Obtain the last two words of the serial number
Memcpy (& szcpusn [8], & uleax, 4 );

_ ASM
{
MoV eax, 3
Cpuid
MoV ulecx, ECx
MoV uledx, EDX
}
// Obtain the first four words of the serial number
Memcpy (& szcpusn [0], & ulecx, 4 );
Memcpy (& szcpusn [4], & uledx, 4 );

// Obtain cpu oem Information
_ ASM
{
MoV eax, 0
Cpuid
MoV dword ptr szcpudesc [0], EBX
MoV dword ptr szcpudesc [4], EDX
MoV dword ptr szcpudesc [8], ECx
}
}
_ Handler T (exception_execute_handler)
{
Return false;
}

Char szcpusnhex [25];
Tohex (szcpusn, 12, szcpusnhex );

Char szcpuid [37];

Sprintf (szcpuid, "% S % s", szcpudesc, szcpusnhex );
Return true;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.