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”。教程

相关文章
相关标签/搜索