title: Running 32 bit assembly code on a 64 bit Linux & 64 bit Processor : Explain the anomaly
body: <p>I'm in an interesting problem.I forgot I'm using 64bit machine &amp; OS and wrote a 32 bit assembly code. I don't know how to write 64 bit code.</p>
  
  <p>This is the x86 32-bit assembly code for Gnu Assembler (AT&amp;T syntax) on Linux. </p>
  
  <pre><code>//hello.S
  #include &lt;asm/unistd.h&gt;
  #include &lt;syscall.h&gt;
  #define STDOUT 1
  
  .data
  hellostr:
      .ascii "hello wolrd\n";
  helloend:
  
  .text
  .globl _start
  
  _start:
      movl $(SYS_write) , %eax  //ssize_t write(int fd, const void *buf, size_t count);
      movl $(STDOUT) , %ebx
      movl $hellostr , %ecx
      movl $(helloend-hellostr) , %edx
      int $0x80
  
      movl $(SYS_exit), %eax //void _exit(int status);
      xorl %ebx, %ebx
      int $0x80
  
      ret
  </code></pre>
  
  <p>Now, This code should run fine on a 32bit processor &amp; 32 bit OS right? As we know 64 bit processors are backward compatible with 32 bit processors. So, that also wouldn't be a problem. The problem arises because of differences in system calls &amp; call mechanism in 64-bit OS &amp; 32-bit OS.  I don't know why but they changed the system call numbers between 32-bit linux &amp; 64-bit linux. </p>
  
  <p>asm/unistd_32.h defines:</p>
  
  <pre><code>#define __NR_write        4
  #define __NR_exit         1
  </code></pre>
  
  <p>asm/unistd_64.h defines:</p>
  
  <pre><code>#define __NR_write              1
  #define __NR_exit               60
  </code></pre>
  
  <p>Anyway using Macros instead of direct numbers is paid off. Its  ensuring correct system call numbers.</p>
  
  <p>when I assemble &amp; link &amp; run the program.</p>
  
  <pre><code>$cpp hello.S hello.s //pre-processor
  $as hello.s -o hello.o //assemble
  $ld hello.o // linker : converting relocatable to executable
  </code></pre>
  
  <p>Its not printing <code>helloworld</code>.</p>
  
  <p>In gdb its showing:</p>
  
  <ul>
  <li>Program exited with code 01.</li>
  </ul>
  
  <p>I don't know how to debug in gdb. using tutorial I tried to debug it and execute instruction by instruction checking registers at each step. its always showing me "program exited with 01". It would be great if some on could show me how to debug this.</p>
  
  <pre><code>(gdb) break _start
  Note: breakpoint -10 also set at pc 0x4000b0.
  Breakpoint 8 at 0x4000b0
  (gdb) start
  Function "main" not defined.
  Make breakpoint pending on future shared library load? (y or [n]) y
  Temporary breakpoint 9 (main) pending.
  Starting program: /home/claws/helloworld 
  
  Program exited with code 01.
  (gdb) info breakpoints 
  Num     Type           Disp Enb Address            What
  8       breakpoint     keep y   0x00000000004000b0 &lt;_start&gt;
  9       breakpoint     del  y   &lt;PENDING&gt;          main
  </code></pre>
  
  <p>I tried running <code>strace</code>. This is its output:</p>
  
  <pre><code>execve("./helloworld", ["./helloworld"], [/* 39 vars */]) = 0
  write(0, NULL, 12 &lt;unfinished ... exit status 1&gt;
  </code></pre>
  
  <ol>
  <li>Explain the parameters of <code>write(0, NULL, 12)</code> system call in the output of strace?</li>
  <li>What <strong>exactly</strong> is happening? I want to know the reason why <strong>exactly</strong> its exiting with exitstatus=1?</li>
  <li>Can some one please show me how to debug this program using gdb?</li>
  <li>Why did they change the system call numbers?</li>
  <li>Kindly change this program appropriately so that it can run correctly on this machine.</li>
  </ol>
  
  <p><strong>EDIT:</strong></p>
  
  <p>After reading Paul R's answer. I checked my files</p>
  
  <pre><code>claws@claws-desktop:~$ file ./hello.o 
  ./hello.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
  
  claws@claws-desktop:~$ file ./hello
  ./hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
  </code></pre>
  
  <p>I agree with him that these should be ELF 32-bit relocatable &amp; executable. But that doesn't answer my my questions. All of my questions still questions. What exactly is happening in this case? Can someone please answer my questions and provide an x86-64 version of this code?</p>