12

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
huggie
  • 17,587
  • 27
  • 82
  • 139
  • Can you give a reference to the book? Or show some samples? – Kurt Pattyn Aug 25 '13 at 14:16
  • 1
    Link is now given. That first program is the one I'm trying to run. – huggie Aug 25 '13 at 14:20
  • I think the best way is to install 32 bit Ubuntu in VirtualBox. There are another options (see https://help.ubuntu.com/community/32bit_and_64bit), but this is really difficult. – Alex F Aug 25 '13 at 14:31

3 Answers3

23

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
4

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.)

Mario Lenz
  • 634
  • 3
  • 8
  • The `--n32` in the man page appears to be a typo for `--x32`. For old code like the stuff in this book, you want `--32`. `--x32` is for newer code that uses x86_64 features but wants 32-bit pointers. When using `as --32` you'll probably also need `-m elf_i386` for the `ld` commands. –  Aug 25 '13 at 14:38
  • @WumpusQ.Wumbley Great it works! I'd check your answer if you'd post one. :) On a side note, is this the recommended way to learn assembly nowadays? I know this book is a bit old, but it seems like a good one. Any 64 bit alternative resource which is just about as good as this book would be helpful. :) – huggie Aug 25 '13 at 14:51
0

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

siddhesh
  • 1
  • 1
  • 1