RSP points not to the top of the stack?
<p>I have a problem understanding how the stack works. First my little code:</p>

<pre><code>void func1 ( int z ) {
  int i = 1;
}

int main ( ) {
  func1 ( 89 );
  return 0;
}
</code></pre>

<p>I am using: 
Ubuntu 16.04 64-bit, 
gcc version 5.4.0, 
gdb version 7.11.1.</p>

<p>I was debugging with GDB, to see how the compiler pushes function arguments on the stack.</p>

<p>When I examine the stack at the point of the where RSP points, I get this:</p>

<pre><code>(gdb) x/10xw $rsp
0x7fffffffdf20: 0xffffdf30  0x00007fff  0x00400525  0x00000000
0x7fffffffdf30: 0x00400530  0x00000000  0xf7a2e830  0x00007fff
0x7fffffffdf40: 0x00000000  0x00000000
</code></pre>

<p>When I print out the address of newest created variable, I get this:</p>

<pre><code>(gdb) p &amp;i
$4 = (int *) 0x7fffffffdf14 
</code></pre>

<p>When I print out the address of the variable, which was hand over to the function, I get this:</p>

<pre><code>(gdb) p &amp;z
$5 = (int *) 0x7fffffffdf0c
</code></pre>

<p>The stack is growing to lower numbers.</p>

<p>So I thought that <code>RSP</code> always points to the top of the stack, meaning that when I i call this command <code>x/10xw $rsp</code> I am able to see all the variables from the function, but I can't see them from there.</p>

<p>The first address after this command is way higher than the address of the variable <code>z</code>. Because of that I was guessing that <code>RSP</code> points not on the top of the stack.</p>

<p>What is also wondering me, is that the address of <code>i</code> is higher than the address of <code>z</code>. 
Since <code>i</code> were later pushed to the stack than <code>z</code>, <code>i</code> must be a lower address than z in my opinion.</p>

<p>I hope someone can explain me why this is so.</p>

<p><strong>EDIT:</strong> I have found the answer!
It was an optimization from the compiler. In <code>func1()</code> the <code>RSP</code> register had not pointed to the "top" of the stack because it was not necessary. It were just necessary if in <code>func1()</code> a other function were called. So the compiler saw that and didn't decrement the <code>RSP</code> register. </p>

<p>Here ismy assembler code with no function call in <code>func1()</code>:</p>

<pre><code>   0x00000000004004d6 &lt;+0&gt;:     push   rbp
   0x00000000004004d7 &lt;+1&gt;:     mov    rbp,rsp
   0x00000000004004de &lt;+8&gt;:     mov    DWORD PTR [rbp-0x14],edi
   0x00000000004004e1 &lt;+11&gt;:    mov    DWORD PTR [rbp-0x4],0x1
   0x00000000004004e8 &lt;+18&gt;:    mov    eax,0x0
   0x00000000004004f3 &lt;+29&gt;:    leave  
   0x00000000004004f4 &lt;+30&gt;:    ret 
</code></pre>

<p>So you can see no <code>SUB</code> call for decrementing <code>RSP</code>.</p>

<p>Now the code from <code>func1()</code> with a function call:</p>

<pre><code>   0x00000000004004d6 &lt;+0&gt;:     push   rbp
   0x00000000004004d7 &lt;+1&gt;:     mov    rbp,rsp
   0x00000000004004da &lt;+4&gt;:     sub    rsp,0x20  
   0x00000000004004de &lt;+8&gt;:     mov    DWORD PTR [rbp-0x14],edi
   0x00000000004004e1 &lt;+11&gt;:    mov    DWORD PTR [rbp-0x4],0x1
   0x00000000004004e8 &lt;+18&gt;:    mov    eax,0x0
   0x00000000004004ed &lt;+23&gt;:    call   0x4004f5 &lt;func2&gt;
   0x00000000004004f2 &lt;+28&gt;:    nop
   0x00000000004004f3 &lt;+29&gt;:    leave  
   0x00000000004004f4 &lt;+30&gt;:    ret    
</code></pre>

<p>So you can see the <code>SUB</code> call for decrementing <code>RSP</code>. So <code>RSP</code> can point to the "top". </p>