32位彙編程序在64位Ubuntu上的彙編和鏈接

本教程使用的操做系統是Ubuntu Linux 18.04 LTS版本,彙編器是GNU AS(簡稱as),鏈接器是GNU LD(簡稱ld)。linux

如下是一段用於檢測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位程序了。bash

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軟件包就能夠了:ui

sudo apt-get install libc6-dev-i386

安裝完成,從新彙編並鏈接完成後運行一下這個程序:操作系統

./cpuid2

命令行輸出爲:命令行

The processor Vendor ID is 'AuthenticAMD'code

因爲這臺機器用的是AMD處理器,因此輸出CPU品牌是「AuthenticAMD」,若是是Intel的CPU,輸出則是「GenuineIntel」。教程

相關文章
相關標籤/搜索