title: Assembly - Passing parameters to a function call
body: <p>I am currently practicing with assembly reading by disassemblying C programs and trying to understand what they do. </p>
  
  <p>I am stuck with a trivial one: a simple hello world program.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main() {
    printf("Hello, world!");
    return(0);
  }
  </code></pre>
  
  <p>When I disassemble the main:</p>
  
  <pre><code>(gdb) disassemble main
  Dump of assembler code for function main:
     0x0000000000400526 &lt;+0&gt;: push   rbp
     0x0000000000400527 &lt;+1&gt;: mov    rbp,rsp
     0x000000000040052a &lt;+4&gt;: mov    edi,0x4005c4
     0x000000000040052f &lt;+9&gt;: mov    eax,0x0
     0x0000000000400534 &lt;+14&gt;:    call   0x400400 &lt;printf@plt&gt;
     0x0000000000400539 &lt;+19&gt;:    mov    eax,0x0  
     0x000000000040053e &lt;+24&gt;:    pop    rbp
     0x000000000040053f &lt;+25&gt;:    ret
  </code></pre>
  
  <p>I understand the first two lines: the base pointer is saved on the stack (by push rbp, which causes the value of the stack pointer to be decreased by 8, because it has "grown") and the value of the stack pointer is saved in the base pointer (so that parameters and local variable can be easily reached through positive and negative offsets, respectively, while the stack can keep "growing").</p>
  
  <p>The third line presents the first issue: why is 0x4005c4 (the address of the "Hello, World!" string) moved in the edi register instead of moving it on the stack? Shouldn't the printf function take the address of that string as parameter? For what I know, functions take parameters from the stack (but here, it looks like the parameter is put in that register: edi)</p>
  
  <p>On another post here on StackOverflow I read that "printf@ptl" is like a stub function that calls the real printf function. I tried to disassemble that function, but it gets even more confusing:</p>
  
  <pre><code>(gdb) disassemble printf
  Dump of assembler code for function __printf:
     0x00007ffff7a637b0 &lt;+0&gt;: sub    rsp,0xd8
     0x00007ffff7a637b7 &lt;+7&gt;: test   al,al
     0x00007ffff7a637b9 &lt;+9&gt;: mov    QWORD PTR [rsp+0x28],rsi
     0x00007ffff7a637be &lt;+14&gt;:    mov    QWORD PTR [rsp+0x30],rdx
     0x00007ffff7a637c3 &lt;+19&gt;:    mov    QWORD PTR [rsp+0x38],rcx
     0x00007ffff7a637c8 &lt;+24&gt;:    mov    QWORD PTR [rsp+0x40],r8
     0x00007ffff7a637cd &lt;+29&gt;:    mov    QWORD PTR [rsp+0x48],r9
     0x00007ffff7a637d2 &lt;+34&gt;:    je     0x7ffff7a6380b &lt;__printf+91&gt;
     0x00007ffff7a637d4 &lt;+36&gt;:    movaps XMMWORD PTR [rsp+0x50],xmm0
     0x00007ffff7a637d9 &lt;+41&gt;:    movaps XMMWORD PTR [rsp+0x60],xmm1
     0x00007ffff7a637de &lt;+46&gt;:    movaps XMMWORD PTR [rsp+0x70],xmm2
     0x00007ffff7a637e3 &lt;+51&gt;:    movaps XMMWORD PTR [rsp+0x80],xmm3
     0x00007ffff7a637eb &lt;+59&gt;:    movaps XMMWORD PTR [rsp+0x90],xmm4
     0x00007ffff7a637f3 &lt;+67&gt;:    movaps XMMWORD PTR [rsp+0xa0],xmm5
     0x00007ffff7a637fb &lt;+75&gt;:    movaps XMMWORD PTR [rsp+0xb0],xmm6
     0x00007ffff7a63803 &lt;+83&gt;:    movaps XMMWORD PTR [rsp+0xc0],xmm7
     0x00007ffff7a6380b &lt;+91&gt;:    lea    rax,[rsp+0xe0]
     0x00007ffff7a63813 &lt;+99&gt;:    mov    rsi,rdi
     0x00007ffff7a63816 &lt;+102&gt;:   lea    rdx,[rsp+0x8]
     0x00007ffff7a6381b &lt;+107&gt;:   mov    QWORD PTR [rsp+0x10],rax
     0x00007ffff7a63820 &lt;+112&gt;:   lea    rax,[rsp+0x20]
     0x00007ffff7a63825 &lt;+117&gt;:   mov    DWORD PTR [rsp+0x8],0x8
     0x00007ffff7a6382d &lt;+125&gt;:   mov    DWORD PTR [rsp+0xc],0x30
     0x00007ffff7a63835 &lt;+133&gt;:   mov    QWORD PTR [rsp+0x18],rax
     0x00007ffff7a6383a &lt;+138&gt;:   mov    rax,QWORD PTR [rip+0x36d70f]        # 0x7ffff7dd0f50
     0x00007ffff7a63841 &lt;+145&gt;:   mov    rdi,QWORD PTR [rax]
     0x00007ffff7a63844 &lt;+148&gt;:   call   0x7ffff7a5b130 &lt;_IO_vfprintf_internal&gt;
     0x00007ffff7a63849 &lt;+153&gt;:   add    rsp,0xd8
     0x00007ffff7a63850 &lt;+160&gt;:   ret    
  End of assembler dump.
  </code></pre>
  
  <p>The two mov operations on eax (mov eax, 0x0) bother me a little as well, since I don't get they role in here (but I am more concerned with what I have just described).
  Thank you in advance.</p>