INTEL处理器识别和CPUID指令(一) CPUID指令与状态寄存器的演变

按:本系列翻译自INTEl官方的CPUID指令文档《Intel® Processor Identification and the CPUID Instruction》,由于我自身英语水平有限,之前顶多也就在上学时翻译过一些英语短文,翻译这种对准确性要求较高的文档,错误在所难免,这次就当做是练笔吧,以后会进行修改完善。

      从INTEL486(*)开始,INTEL处理器提供了一个直接的方法来检测其内部架构能否执行CPUID指令。该方法利用标志寄存器EFLAGS的第21位(最左边为第31位,最右边为第0位)进行检测。如果应用程序(原文为sofaware)能够修改这个标志位的值,则CPUID指令能够执行(如下图所示)。PUSHF、PUSHFD指令和POPF、POPFD指令用于访问EFLAGS寄存器中的状态信息。本文档后面的程序演示了如何用PUSHFD指令和POPFD指令读写EFLAGS寄存器的ID标志位(即第21位)。

状态寄存器的演变

*实际上 只有部分486TM处理器和后续的INTEL处理器能够执行CPUID指令。INTEL 386TM处理器的EFLAGS寄存器的第21位是不能被应用程序(原文为sofaware)修改的,并且不能执行CPUID指令。在不支持CPUID指令的处理器上执行该指令会导致一个“invalid opcode”异常。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用汇编代码实现CPUID指令并输出处理器识别字符串的示例: ```assembly section .data cpuid_string db "Processor identification string: ", 0 buffer db 13,10, " ", 0 eax_value dd 0 ebx_value dd 0 ecx_value dd 0 edx_value dd 0 section .text global _start _start: ; print the CPUID string mov eax, 0x80000002 ; set EAX to start of processor identification string mov esi, buffer ; set ESI to point to buffer for storing identification string mov edi, eax_value ; set EDI to point to buffer for storing the four DWORDs returned by CPUID ; execute CPUID four times to get all four DWORDs of the identification string cpuid ; first execution of CPUID mov dword [edi], eax ; store first DWORD in buffer mov dword [edi+4], ebx ; store second DWORD in buffer mov dword [edi+8], ecx ; store third DWORD in buffer mov dword [edi+12], edx ; store fourth DWORD in buffer add edi, 16 ; move buffer pointer to next 16 bytes cmp eax, 0x80000004 ; check if we need to execute CPUID again jb print_string ; if not, print the identification string cpuid ; second execution of CPUID mov dword [edi], eax ; store first DWORD in buffer mov dword [edi+4], ebx ; store second DWORD in buffer mov dword [edi+8], ecx ; store third DWORD in buffer mov dword [edi+12], edx ; store fourth DWORD in buffer add edi, 16 ; move buffer pointer to next 16 bytes cmp eax, 0x80000005 ; check if we need to execute CPUID again jb print_string ; if not, print the identification string cpuid ; third execution of CPUID mov dword [edi], eax ; store first DWORD in buffer mov dword [edi+4], ebx ; store second DWORD in buffer mov dword [edi+8], ecx ; store third DWORD in buffer mov dword [edi+12], edx ; store fourth DWORD in buffer add edi, 16 ; move buffer pointer to next 16 bytes cmp eax, 0x80000006 ; check if we need to execute CPUID again jb print_string ; if not, print the identification string cpuid ; fourth execution of CPUID mov dword [edi], eax ; store first DWORD in buffer mov dword [edi+4], ebx ; store second DWORD in buffer mov dword [edi+8], ecx ; store third DWORD in buffer mov dword [edi+12], edx ; store fourth DWORD in buffer print_string: ; print the CPUID string mov eax, 4 ; set EAX to 4 to call the "write" system call mov ebx, 1 ; set EBX to 1 to specify stdout mov ecx, cpuid_string ; set ECX to point to the CPUID string to print int 0x80 ; call the system call to print the string mov eax, 4 ; set EAX to 4 to call the "write" system call mov ebx, 1 ; set EBX to 1 to specify stdout mov ecx, buffer ; set ECX to point to the buffer containing the identification string int 0x80 ; call the system call to print the identification string exit: ; exit the program mov eax, 1 ; set EAX to 1 to call the "exit" system call xor ebx, ebx ; set EBX to 0 to indicate success int 0x80 ; call the system call to exit the program ``` 这段代码会使用CPUID指令获取处理器识别字符串,并将其输出到控制台中。你可以将它保存为一个文件(例如`cpuid.asm`),使用汇编器和链接器将其编译成可执行文件,然后在终端中运行它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值