title: os x 32-bit printf from assembler
body: <p>I'm back again - thanks to the kind folks here, especially @Jester and @PeterCordes, I have three out of my four target platforms working. I have Win 32 and Win 64 working, and I have OS X 64 working, but I can't seem to make OS X assembly work in 32-bit using the libc library.</p>
  
  <p>I can do a "hello world" and other things using syscall (64-bit) and int 0x80 (32-bit), and I can make _printf work in 64-bit, but I can't manage to make it work in 32-bit.</p>
  
  <p>In theory, I need to push the parameters onto the stack and then 16-byte align the stack, but I get a segmentation fault: 11 error when I run the code.</p>
  
  <pre><code> 8 section .data
   9 
  10 hello   db  "Hello, world", 0x0a, 0x00
  11 
  12 section .text
  13 
  14 global _main
  15 extern _printf, _exit
  16 
  17 _main:
  18     push hello
  19     sub esp, 12     ; 16-byte align stack
  20     call _printf
  21 
  22     add esp, 16     ; undo stack alignment
  23     push 0
  24     sub esp, 12     ; 16-byte align stack
  25     call _exit
  </code></pre>
  
  <p>I've tried it without the stack aligning code and I get the same error. I've found various samples online and I could not make them work, and I've even done the trick with using GCC to output Intel-syntax assembler and I could not manage to translate it to nasm.</p>
  
  <p>I'm using nasm because it works on Windows, OS X, and Linux, so that way I only need to learn one assembler syntax.</p>
  
  <p>I realize that all the Intel Macs are 64-bit, but they can run 32-bit code so I want to be able to follow it and understand it. This is all aimed at helping me reverse-engineer malware.</p>
  
  <p>Thanks</p>