Linux 在 64 位 ubuntu 上编译 32 位汇编程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13178501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:40:40  来源:igfitidea点击:

Compiling 32 bit Assembler on 64 bit ubuntu

linuxubuntuassembly64-bit32-bit

提问by Klemenko

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:

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

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

output:

输出:

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

采纳答案by iabdalkader

First change _printfto printfand the _startsymbol to main, then use gccto 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 :

首先更改_printfprintf并且_start符号为main,然后用于gcc链接目标文件,该文件将自动链接到libc您需要这样做,因为AFAIK 没有main. 此外,您应该在组装时使用 elf32 而不是 elf64,因为代码有 32 位指令:

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

And build with:

并构建:

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

Edit:

编辑:

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

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

apt-get install ia32-libs 

回答by Nikos C.

It looks to me like you forgot to link against the C library, which is the part that provides the printffunction (and others):

在我看来,您好像忘记链接 C 库,这是提供printf功能(和其他功能)的部分:

ld vaja4.o -o vaja4 -lc

回答by binW

I doubt that the error you see is because of 32/64 bit issue. The error that you see i.e

我怀疑您看到的错误是因为 32/64 位问题。你看到的错误即

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

is clearly telling you the symbol _printf is undefined which means that the library for printf function is not being linked.

清楚地告诉您符号 _printf 未定义,这意味着未链接 printf 函数库。

your linking step i.e

你的链接步骤即

ld vaja4.o -o vaja4

does not include any libraries. You need to link your program with a library that can provide definition of the printf function. I believe ld should pick the library it self without bothering you with these messages but because it is not able to find a suitable C library for this function, I guess you dont have the required libraries i.e either 32 bit or 64 library is missing.

不包括任何库。您需要将您的程序与可以提供 printf 函数定义的库链接起来。我相信 ld 应该自己选择库而不用这些消息来打扰您,但是因为它无法为该函数找到合适的 C 库,我猜您没有所需的库,即缺少 32 位或 64 位库。

Anyway, plz try the following sequence of commands to assemble and link your program:

无论如何,请尝试以下命令序列来组装和链接您的程序:

nasm -f elf vaja4.asm
ld -m elf_i386 vaja4.o vaja4
./vaja4

回答by bit_pusher

On Ubuntu 12.10, you need to install development packages first

在 Ubuntu 12.10 上,需要先安装开发包

sudo apt-get update
sudo apt-get install libc6-dev-i386

for

为了

gcc -m32 vaja4.o -o vaja4

to work.

上班。