title: Assemble and run i386 asm program on x86-64 Linux with as and ld
body: <p>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).</p>
  
  <p>I know there are similar questions answered already, But none of them use <code>as</code> and <code>ld</code> to do it so I don't know exactly how could I translate those solutions to my problem.</p>
  
  <p>For the x86-64 I use <code>as</code> and <code>ld</code> as follows:</p>
  
  <pre><code>#       Assemble: as exit.s -o exit.o
  #        Linking: ld exit.o -o exit
  </code></pre>
  
  <p>The x86-32 version of the program:</p>
  
  <pre><code>.section .data
  .section .text
  .globl _start
  _start:
      movl $1, %eax
      movl $0, %ebx
      int $0x80
  </code></pre>
  
  <p>Now.. I've been searching how to do it and found about the <code>--32</code> in as and the <code>-m {arg}</code> in <code>ld</code> but every time I get to compile it without arch errors it gives me the "File in wrong format" error. </p>
  
  <p>I've tried doing ld with elf_i386 and i386linux like this:</p>
  
  <pre><code>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
  </code></pre>
  
  <hr>
  
  <p>I want to add that for compatibility I've installed these packages listed in the Ubuntu help forum:</p>
  
  <pre><code>sudo dpkg --add-architecture i386
  sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386
  sudo apt-get install multiarch-support
  </code></pre>