This question already has an answer here:
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'
I doubt that the error you see is because of 32/64 bit issue. The error that you see i.e
is clearly telling you the symbol _printf is undefined which means that the library for printf function is not being linked.
your linking step i.e
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.
Anyway, plz try the following sequence of commands to assemble and link your program:
It looks to me like you forgot to link against the C library, which is the part that provides the
printf
function (and others):First change
_printf
toprintf
and the_start
symbol tomain
, then usegcc
to link the object file, which will automatically link it tolibc
, you need to do that because AFAIK you can't link to libc without amain
. Also you should use elf32 not elf64 when assembling because the code has 32 bits instructions :And build with:
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
On Ubuntu 12.10, you need to install development packages first
for
to work.