在 64 位 ubuntu 上编译 32 位汇编程序 [英] Compiling 32 bit Assembler on 64 bit ubuntu

查看:28
本文介绍了在 64 位 ubuntu 上编译 32 位汇编程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用 32 位汇编语言编写的程序...现在我无法在 64 位操作系统上编译它.在我们学校,它们是特定的,程序必须用 32 位版本编写.这是我的程序:

I have program written in 32 bit assembly language... Now I just can't compile it on 64 bit OS. On our school they are specific and program has to be written in 32 bit version. Here is my program:

bits 32
extern _printf
global _start

section .data
    message db "Hello world!!", 10, 0

section .text

_start:
    pushad 
    push dword message
    call _printf 
    add esp, 4 
    popad 
    ret

有什么想法吗?我已经尝试了很多方法来编译它.编译后错误输出:

Any idea? I have tried so many ways to compile that. Error output after compiling:

nasm -f elf64 vaja4.asm
ld vaja4.o -o vaja4
./vaja4

输出:

vaja4.o: In function `_start':
vaja4.asm:(.text+0x7): undefined reference to `_printf'

推荐答案

首先将_printf改为printf,将_start符号改为main,然后使用gcc链接目标文件,它会自动链接到libc你需要这样做,因为AFAIK你可以不链接到没有 main 的 libc.另外你应该在组装时使用 elf32 而不是 elf64,因为代码有 32 位指令:

First change _printf to printf and the _start symbol to main, then use gcc to link the object file, which will automatically link it to libc, you need to do that because AFAIK you can't link to libc without a main. Also you should use elf32 not elf64 when assembling because the code has 32 bits instructions :

bits 32
extern printf
global main

section .data
    message db "Hello world!!", 10, 0

section .text

main:
    pushad 
    push dword message
    call printf 
    add esp, 4 
    popad 
    ret

并构建:

nasm -f elf32 vaja4.asm
gcc -m32 vaja4.o -o vaja4
$./test 
$Hello world!!

由于您现在要在 64 位系统上编译 32 位代码,因此您需要安装 32 位版本的库

Since you're now compiling 32-bit code on a 64-bit system, you will need to install the 32-bit version of the libraries

apt-get install ia32-libs 

这篇关于在 64 位 ubuntu 上编译 32 位汇编程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆