title: Accessing malloc'd memory in assembly
body: <p>I'm trying to access memory I have malloced in assembly but I keep just repeatedly getting segfault errors. What am I doing wrong in the following code, I'm sure it's simple but I just can't  see it!</p>
  
  <p><strong>EDIT:</strong> I am using 64 bit NASM assembly</p>
  
  <pre><code>; Allocate room for 8 integers
  mov r8, 8
  mov rdi, r8
  imul rdi, 8 ; Multiply by 8 (8 bytes per entry in 64bit)
  xor rax, rax
  call malloc
  add rsp, 8
  test rax, rax
  jz malloc_failure
  mov r8, rsp
  
  ; r8 now = base of array
  
  ; Set the first element to be 100
  mov r9, 0
  add r9, r8
  mov qword [r9], 100
  
  malloc_failure:
  deallocate_start:
  dealloc_1:
  mov rdi, r8
  xor rax, rax
  call free
  add rsp, 8
  deallocate_end:
  call os_return      ; return to operating system
  </code></pre>
  
  <p>And the segfault (Not very interesting...)</p>
  
  <pre><code>matrix05% ./arr5
  Segmentation fault
  </code></pre>