Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling 32 bit Assembler on 64 bit ubuntu [duplicate]

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'
like image 603
Klemenko Avatar asked Nov 01 '12 13:11

Klemenko


2 Answers

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!!

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 
like image 75
iabdalkader Avatar answered Sep 19 '22 13:09

iabdalkader


On Ubuntu 12.10, you need to install development packages first

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

for

gcc -m32 vaja4.o -o vaja4

to work.

like image 44
bit_pusher Avatar answered Sep 23 '22 13:09

bit_pusher