title: How is the stack pointer changed in this program with call and ret
body: <p>My questions pertain to the actions that seem to happen between the line when context is changed especially concerning <code>RSP</code> and <code>RBP</code>.</p>
  
  <p>Given this very simple program:</p>
  
  <pre><code>Reading symbols from ./function_call...done.
  (gdb) disass main
  Dump of assembler code for function main:
     0x00000000004004d6 &lt;+0&gt;: push   rbp
     0x00000000004004d7 &lt;+1&gt;: mov    rbp,rsp
     0x00000000004004da &lt;+4&gt;: mov    esi,0x2
     0x00000000004004df &lt;+9&gt;: mov    edi,0x1
     0x00000000004004e4 &lt;+14&gt;:    call   0x4004b6 &lt;add_and_7&gt;
     0x00000000004004e9 &lt;+19&gt;:    mov    eax,0x0
     0x00000000004004ee &lt;+24&gt;:    pop    rbp
     0x00000000004004ef &lt;+25&gt;:    ret    
  End of assembler dump.
  (gdb) disass add_and_7
  Dump of assembler code for function add_and_7:
     0x00000000004004b6 &lt;+0&gt;: push   rbp
     0x00000000004004b7 &lt;+1&gt;: mov    rbp,rsp
     0x00000000004004ba &lt;+4&gt;: mov    DWORD PTR [rbp-0x14],edi
     0x00000000004004bd &lt;+7&gt;: mov    DWORD PTR [rbp-0x18],esi
     0x00000000004004c0 &lt;+10&gt;:    mov    DWORD PTR [rbp-0x4],0x7
     0x00000000004004c7 &lt;+17&gt;:    mov    edx,DWORD PTR [rbp-0x14]
     0x00000000004004ca &lt;+20&gt;:    mov    eax,DWORD PTR [rbp-0x18]
     0x00000000004004cd &lt;+23&gt;:    add    edx,eax
     0x00000000004004cf &lt;+25&gt;:    mov    eax,DWORD PTR [rbp-0x4]
     0x00000000004004d2 &lt;+28&gt;:    add    eax,edx
     0x00000000004004d4 &lt;+30&gt;:    pop    rbp
     0x00000000004004d5 &lt;+31&gt;:    ret    
  End of assembler dump.
  (gdb) list
  1   int add_and_7( int num1, int num2 ) {
  2       int seven = 7;
  3       return num1 + num2 + seven;
  4   }
  5   
  6   int main() {
  7       add_and_7( 1, 2 );
  8       return 0;
  9   }
  </code></pre>
  
  <p>All functions start off with push <code>rbp</code> which I as I understand it is preserving the parent context onto the stack. How does the parent function know how to rebuild itself? Are the necessary steps built into <code>call</code> and <code>ret</code>?</p>
  
  <p>Then the <code>rsp</code> is always moved to <code>rbp</code>. As I have read this sets the new stack base to be in the context of the current function. What I can't seem to figure out is when or how stack pointer was set to that point in the first place. My best guess is the assembly function call does this, is that whats happening?</p>
  
  <p>Lastly when a method returns it seems like <code>eax</code> is the register that is used for the parent function to utilize the return of its child function. Is <code>eax</code> explicitly used for this or is this just a convention with my compiler and architecture?</p>