So I'm new trying asm and I want to compile a little program that just exits with i386 instructions instead of x86-64 ones. I have a x86-64 Ubuntu which compile and run x86-64 one perfectly but I don't get it how to assemble and link the i386 version on the same x86-64 machine (I have installed i386 compat).
I know there are similar questions answered already, But none of them use as
and ld
to do it so I don't know exactly how could I translate those solutions to my problem.
For the x86-64 I use as
and ld
as follows:
# Assemble: as exit.s -o exit.o
# Linking: ld exit.o -o exit
The x86-32 version of the program:
.section .data
.section .text
.globl _start
_start:
movl $1, %eax
movl $0, %ebx
int $0x80
Now.. I've been searching how to do it and found about the --32
in as and the -m {arg}
in ld
but every time I get to compile it without arch errors it gives me the "File in wrong format" error.
I've tried doing ld with elf_i386 and i386linux like this:
as --32 exit.s -o exit.o
ld -m elf_i386 exit.o -o exit
#Error: -bash: ./exit: cannot execute binary file: File in wrong format
ld -m i386linux exit.o -o exit
#Error: -bash: ./exit: cannot execute binary file: File in wrong format
I want to add that for compatibility I've installed these packages listed in the Ubuntu help forum:
sudo dpkg --add-architecture i386
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386
sudo apt-get install multiarch-support