C++擷取CPU ID

來源:互聯網
上載者:User

以前一直以為每個CPU都有唯一的CPU ID,查了一些參考資料後才知道,對於Intel的cpu,pentium III以後就不再支援唯一的cpu標識了,只可查到有廠家、型號和支援的功能等資訊。

下面是用cpuid指令擷取這些資訊的參考資料。

閑人的這篇博文一定要看:《在C++中使用cpuid指令獲得CPU資訊

原文連結:http://www.cnblogs.com/freeman/archive/2005/08/30/226128.html

他幫我們封裝了一個c++類,用來調用cpuid,擷取相關資訊。對於擷取到的資訊的解讀,可以參考天壯的部落格《cpuid的用途》

原文連結:http://fellowher.blog.163.com/blog/static/5076351520091017111313946/?fromdm&fromSearch&isFromSearchEngine=yes

和這個連結:http://siyobik.info/main/reference/instruction/CPUID

由於閑人寫的類中用的彙編代碼是Intel格式的,mingw只支援AT&T格式的代碼,所以如果你需要用mingw編譯的話還需要把其中的彙編代碼改為AT&T格式,下面是改寫後AT&T格式的彙編代碼:

Intel格式:

 __asm
 {
  mov eax, veax
  cpuid
  mov deax, eax
  mov debx, ebx
  mov decx, ecx
  mov dedx, edx
 }

AT&T格式:

 __asm__("movl %4, %�x\n\t"
  "cpuid\n\t"
  "movl %�x, %0\n\t"
  "movl %�x, %1\n\t"
  "movl %�x, %2\n\t"
  "movl %�x, %3"
  :"=m"(deax),"=m"(debx),"=m"(decx),"=m"(dedx)  
  :"r"(veax) 
  :"�x","�x","�x","�x"
  );

關於mingw中嵌入彙編代碼的文法可參考:http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

 

轉自:http://blog.sina.com.cn/s/blog_4f183d960100swcp.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.