• Uncategorized

About assembly : Compiling-32-bit-assembly-on-64bit-system-Ubuntu

Question Detail

I’m trying to learn about assembly with the book “Programming from the ground up”. The book covers only 32 bit instructions. Is there a way to run the example codes on 64 bit Ubuntu system? I can’t understand the stuff on the man page of the GNU assembler but I heard the -m32 flag should do it. But it’s not a recognized option.

How do I get the examples on the book to work with ease?

Question Answer

When your assembler and linker are x86_64 versions, the options to produce i386 (32-bit) output are

as --32
ld -m elf_i386

You don’t have to use as and ld just because you’re working with assembly code. gcc can be used, and in that case you would use -m32.

gcc -m32 -nostdlib myprog.s -o myprog

From the as man page:

   Target i386 options:
      [--32|--n32|--64] [-n]
      [-march=CPU[+EXTENSION...]] [-mtune=CPU]

I’m not sure if it works, just try --32 or --n32.

(-m32 seems to ge the corresponding gcc flag.)

Creating a 32-bit executable on a 64-bit PC requires that you “warn” the linker that a 32-bit elf file is coming:

$ nasm -f elf -g -F stabs eat.asm

$ ld -o eat eat.o -melf_i386

That’s what the melf_i386 directive does: It tells ld that the eat.o file is an elf32 linkable object file. The invocation of NASM is the same as you’d use on a 32-bit PC.

Thanks @Jeff Duntemann

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.