本教程使用的操作系統是Ubuntu Linux 18.04 LTS版本,匯編器是GNU AS(簡稱as),連接器是GNU LD(簡稱ld)。
以下是一段用于檢測CPU品牌的匯編小程序(cpuid2.s):
.section .data
output:
.asciz "The processor Vendor ID is '%s'\n"
.section .bss
.lcomm buffer, 12
.section .text
.globl _start
_start:
movl $0, %eax
cpuid
movl $buffer, %edi
movl %ebx, (%edi)
movl %edx, 4(%edi)
movl %ecx, 8(%edi)
pushl $buffer
pushl $output
call printf
addl $8, %esp
pushl $0
call exit
由于這是一個32位代碼,并不能直接編譯成64位程序,那么只能編譯成32位程序了。
as -32 -gstabs -o cpuid2.o cpuid2.s
ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.o
提示:ld: i386 架構于輸入文件 cpuid2.o 與 i386:x86-64 輸出不兼容
解決方法很簡單,只要安裝libc6-dev-i386軟件包就可以了:
sudo apt-get install libc6-dev-i386
安裝完成,重新匯編并連接完成后運行一下這個程序:
./cpuid2
命令行輸出為:
The processor Vendor ID is 'AuthenticAMD'
由于這臺機器用的是AMD處理器,所以輸出CPU品牌是“AuthenticAMD”,如果是Intel的CPU,輸出則是“GenuineIntel”。