title: OS X - x64: stack not 16 byte aligned error
body: <p>I know that OS X is 16 byte stack align, but I don't really understand why it is causing an error here. </p>
  
  <p><strong>All I am doing here is to pass an object size (which is 24) to %rdi, and call malloc. Does this error mean I have to ask for 32 bytes ?</strong></p>
  
  <p>And the error message is:</p>
  
  <blockquote>
    <p>libdyld.dylib`stack_not_16_byte_aligned_error:
    ->  0x7fffc12da2fa &lt;+0>: movdqa %xmm0, (%rsp)
        0x7fffc12da2ff &lt;+5>: int3</p>
    
    <p>libdyld.dylib`_dyld_func_lookup:
        0x7fffc12da300 &lt;+0>: pushq  %rbp
        0x7fffc12da301 &lt;+1>: movq   %rsp, %rbp</p>
  </blockquote>
  
  <p>Here is the code:</p>
  
  <pre><code>Object_copy:
      pushq %rbp
      movq %rbp, %rsp
  
      subq $8, %rsp
      movq %rdi, 8(%rsp)          # save self address
      movq obj_size(%rdi), %rax   # get object size
      imul $8, %rax          
      movq %rax, %rdi 
      callq _malloc             &lt;------------------- error in this call
  
      # rsi old object address
      # rax new object address
      # rdi object size, mutiple of 8
  
      # rcx temp reg
  
      # copy object tag
      movq 0(%rsi), %rcx
      movq %rcx, 0(%rax)
  
      # set rdx to counter, starting from 8
      movq $8, %rdx
  
      # add 8 to object size, since we are starting from 8
      addq $8, %rdi
  
      start_loop:
          cmpq %rdx, %rdi
          jle end_loop
  
          movq (%rdx, %rsi, 1), %rcx
          movq %rcx, (%rdx, %rax, 1)
  
          addq $8, %rdx
          jmp start_loop
  
      end_loop:
          leave 
          ret
  
  
  
  Main_protoObj:
      .quad    5                          ; object tag
      .quad    3                          ; object size
      .quad    Main_dispatch_table        ; dispatch table
  
  _main:
      leaq Main_protoObj(%rip), %rdi
      callq Object_copy                # copy main proto object
      subq $8, %rsp                    # save the main object on the stack
      movq %rax, 8(%rsp)
      movq %rax, %rdi                 # set rdi point to SELF
      callq Main_init
      callq Main_main
  
      addq $8, %rsp                    # restore stack
  
      leaq _term_msg(%rip), %rax
      callq _print_string
  </code></pre>