title: Compiling NASM on Mac OSX
body: <p>Writing a compiler in school, last milestone is generating assembly code. Trying to learn NASM. Starting at the beginning, <a href="http://www.cs.lmu.edu/~ray/notes/nasmexamples/" rel="noreferrer">http://www.cs.lmu.edu/~ray/notes/nasmexamples/</a>, trying to compile a Hello World.</p>
  
  <pre><code>; ----------------------------------------------------------------------------
  ; helloworld.asm
  ;
  ; This is a Win32 console program that writes "Hello, World" on one line and
  ; then exits.  It needs to be linked with a C library.
  ; ----------------------------------------------------------------------------
  
      global  _main
      extern  _printf
  
      section .text
  _main:
      push    message
      call    _printf
      add     esp, 4
      ret
  message:
      db      'Hello, World', 10, 0
  </code></pre>
  
  <p>To assemble, link and run this program under Windows:</p>
  
  <pre><code>nasm -fwin32 helloworld.asm
  gcc helloworld.obj
  a
  </code></pre>
  
  <p>Under Linux, you'll need to remove the leading underscores from function names, and execute</p>
  
  <pre><code>nasm -felf helloworld.asm
  gcc helloworld.o
  ./a.out
  </code></pre>
  
  <p>But I'm on OSX. Found this little resource: <a href="http://salahuddin66.blogspot.com/2009/08/nasm-in-mac-os-x.html" rel="noreferrer">http://salahuddin66.blogspot.com/2009/08/nasm-in-mac-os-x.html</a>. In Mac OS X we should use format macho...</p>
  
  <pre><code>nasm -f macho -o hello.o hello.asm
  </code></pre>
  
  <p>...and for the linker (we need to specify the entry point)...</p>
  
  <pre><code>ld -e main -o hello hello.o
  </code></pre>
  
  <p>But when I do this...</p>
  
  <pre><code>Undefined symbols:
      "printf", referenced from:
          _main in hello.o
  ld: symbol(s) not found for inferred architecture i386
  </code></pre>
  
  <p>Sorry, I know it's a lot to read. And I doubt there are many NASM coders around these parts, but worth a try right? I'd appreciate any help I can get.</p>