title: How to use malloc and free in 64-bit NASM?
body: <p>In 64-bit NASM, I'm allocating a memory block of 8000 bytes using malloc() from the C library, and when I'm finished with it, I deallocate it by calling free().  </p>
  
  <p>My research has come up with a lot of conflicting information about how to do this in 64-bit NASM, and much of the information is 32-bit, where the calling convention is different, or it's C or C++, not NASM.  </p>
  
  <p>I think I have the malloc part right, but I'm not sure about the free part.  I'm posting this question because I don't want to test it and have a memory block allocated but not freed.  </p>
  
  <p>So my two questions are simple:<br>
      (1) do I have this right for 64-bit NASM?<br>
      (2) is the syntax the same for Windows and Linux?</p>
  
  <p>I'm showing only the malloc and free parts of my program:</p>
  
  <pre><code>extern malloc
  extern free
  
  push rdi
  
  ; Allocate the memory buffer
  mov rdi,8000
  call malloc
  mov [array_pointer],rax ;array_pointer is initialized in .data
  
  ; Code that uses the buffer goes here.  
  
  ; Free the memory buffer
  push rdi
  call free
  add rsp,8
  
  pop rdi
  ret
  </code></pre>