32位汇编程序在64位Ubuntu上的汇编和连接

本教程使用的操作系统是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”。

转载于:https://my.oschina.net/u/943779/blog/1861326

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值