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

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

问题描述

我编写的程序在32位汇编​​语言......现在,我只是不能在64位操作系统上进行编译。在我们学校,他们是具体的,程序必须使用32位版本。这是我的计划:

 位32
EXTERN _printf
全球_start

段.data
    消息分贝世界,你好!,10,0

段.text

_开始:
    PUSHAD
    推DWORD消息
    拨打_printf
    ADD ESP,4
    POPAD
    RET
 

你知道吗?我已经试了很多方法来编译。 编译后的错误输出:

  NASM -f ELF64 vaja4.asm
LD vaja4.o -o vaja4
./vaja4
 

输出:

  vaja4.o:在功能`_start:
vaja4.asm :(文字+ 0x7的):未定义的参考`_printf
 

解决方案

第一个变化 _printf 的printf _start 符号,然后用 GCC 来链接目标文件,它会自动链接到的libc ,你需要做的,因为AFAIK你不能链接没有主要到libc 的。你也应该使用ELF32组装,因为code的32位指令时不能ELF64:

 位32
EXTERN的printf
全球主要的

段.data
    消息分贝世界,你好!,10,0

段.text

主要:
    PUSHAD
    推DWORD消息
    调用printf
    ADD ESP,4
    POPAD
    RET
 

和构建具有:

  NASM -f ELF32 vaja4.asm
GCC -m32 vaja4.o -o vaja4
$ /测试
$世界,你好!
 

有一个很好的教程:

http://jdefr.swippet.com/2012 / 03/22 /使用-libc中,与组装/

编辑:

既然你现在编译32位code在64位系统,则需要安装32位版本的库

  apt-get的安装IA32-库
 

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

output:

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

解决方案

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

And build with:

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

There's a nice tutorial here:

http://jdefr.swippet.com/2012/03/22/using-libc-with-assembly/

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

apt-get install ia32-libs 

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

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