Correct understanding of the C ++ cpuid command

Source: Internet
Author: User

The C ++ programming language has a wide range of applications. Many of these functions can help us meet certain requirements and improve programming efficiency to a certain extent. Here we will introduce some application methods for the C ++ cpuid command for your convenience.

  • Overview of basic concepts of C ++ class template Specialization
  • Understand the basic concepts of C ++ non-type template parameters
  • Summary of precautions for using non-type parameters in C ++ function templates
  • C ++ typename different application methods
  • C ++ memory allocation skills

1. What is the C ++ cpuid command?

The CPUID command is an assembly command for obtaining CPU information in intel IA32 architecture. It can be used to obtain CPU-related items such as CPU type, model, manufacturer information, trademark information, serial number, and cache.

2. Use of the C ++ cpuid command

Cpuid uses eax as the input parameter, eax, ebx, ecx, and edx as the output parameter. For example:

 
 
  1. __asm  
  2. {  
  3. mov eax, 1  
  4. cpuid  
  5. ...  

The above Code takes 1 as the input parameter. After the cpuid is executed, the values of all registers are filled with the returned values. For the eax values of different input parameters, the output parameters have different meanings. To better use the cpuid command in C ++, classes can be used to encapsulate the command. A special function is defined in the class to execute the cpuid, and an input parameter is required. You also need to define four member variables to store the values returned after the C ++ cpuid command is executed. Since these four registers are 32-bit long, you can use the unsinged long type variable storage.

 
 
  1. Typedef unsigned long DWORD
  2. Class CPUID
  3. {
  4. Public:
  5. ...
  6. Private:
  7. Void Executecpuid (DWORD eax); // used to implement cpuid
  8. DWORD m_eax; // store the returned eax
  9. DWORD m_ebx; // store the returned ebx
  10. DWORD m_ecx; // store the returned ecx
  11. DWORD m_edx; // store the returned edx
  12. ...
  13. }
  14. Void CPUID: Executecpuid (DWORD veax)
  15. {
  16. // Because embedded assembly code cannot identify class member variables
  17. // Define four temporary variables as transition
  18. DWORD deax;
  19. DWORD debx;
  20. DWORD decx;
  21. DWORD dedx;
  22. _ Asm
  23. {
  24. Mov eax, veax; move the input parameters to eax
  25. Cpuid; run cpuid
  26. Mov deax and eax; the following four lines of code store the variables in the Register into temporary variables:
  27. Mov debx, ebx
  28. Mov decx, ecx
  29. Mov dedx, edx
  30. }
  31. M_eax = deax; // put the content in the temporary variable into the class member variable
  32. M_ebx = debx;
  33. M_ecx = decx;
  34. M_edx = dedx;
  35. }

In this way, you can directly call the Executecupid () function to execute the C ++ cpuid command. The returned values include m_eax, m_ebx, m_ecx, and m_edx.

Related Article

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.