title: mmap in nasm using int 0x80 on linux x86
body: <p>I'm writing a small library in nasm for linux and I'm implementing malloc atm.</p>
  
  <p>The C representation of the code would look like</p>
  
  <pre><code>void * malloc(int size) {
      return mmap(0, size, 3, 34, -1, 0);
  }
  </code></pre>
  
  <p>Now I have to convert that to assembly, but I'm stuck at loading the arguments.</p>
  
  <pre><code>mov eax, 90
  ; load args (help)
  int 0x80
  ; move adress to eax
  </code></pre>
  
  <p>I'd like to know how to convert the C code into asm. In most cases the arguments of a syscall are mapped in the same order as in assembly, but mmap takes 6 arguments, and the kernel takes a maximum of 5 arguments.</p>
  
  <p>I'd also like to know where the returned value is stored.</p>
  
  <p>So, how should I implement it.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>code:</p>
  
  <pre><code>section .data
  mmap_arg:  ; ugly
    .addr:   dq 0
    .len:    dq 512
    .prot:   dq 3
    .flags:  dq 34
    .fd:     dq -1
    .offset: dq 0
  
  mmap_test:
    mov eax, 90
    mov ebx, mmap_arg
    int 0x80
  </code></pre>
  
  <p>It seems like its working, but when I load something at the address of eax, I get a segfault. When I replace eax with ebx, it runs without errors. <strike>Can someone confirm that the returned address is in ebx, or that I'm writing in the .data section when using ebx.</strike></p>
  
  <p>Gdb shows that only eax gets modified after int 0x80. I'm stuck again. It's gettting late, I'll have a look again at it tomorrow.</p>