title: Questions regarding base pointer and stack pointer
body: <p>I am writing a report to summarize stack. If you click on my profile you will see that I have been doing this for a while. Right now, I have some troubles because on GDB it shows me a different thing than on visual studio.</p>
  
  <p>As a result, I am not too sure about my understanding of base pointer and stack pointer, and I am hoping that someone can lead me in the right direction if I am wrong.</p>
  
  <p>For x86 computer, stack is typical growing downward (from higher memory address to lower). </p>
  
  <p>So when a program begins, we called the main function. </p>
  
  <ol>
  <li><p>In general, at the entry of each function call, a stack is created at the current esp location, and this is what we called "the top of the stack". Is this correct?</p></li>
  <li><p>When the old ebp gets pushed onto the stack, is it pushed onto where the esp was first pointed to? </p></li>
  <li><p>Afterward, the esp will move down to point to an empty memory location, is that correct?</p></li>
  <li><p>Finally, esp is always changing, moving down pointing at the next available memory space. Is that correct?</p></li>
  <li><p>Does esp move per byte, or per 4 bytes down?</p></li>
  </ol>
  
  <p>I know there's a lot of questions. But thanks for your time!</p>
  
  <hr>
  
  <p>Thank you for the response, sir!</p>
  
  <p>@iSciurus</p>
  
  <p>I am confused how everyone define esp pointing at the most recent entry that was pushed onto the stack. </p>
  
  <p>For x86, since the stack grows downward, from your explanation, the esp will first point at the lowest address of the stack.  When I look at the the assembly code, we have</p>
  
  <pre><code>   0x080483f4 &lt;+0&gt;: push   %ebp
     0x080483f5 &lt;+1&gt;: mov    %esp,%ebp
     0x080483f7 &lt;+3&gt;: sub    $0x10,%esp
  </code></pre>
  
  <p>So esp is decremented 16 bytes. So this is the <strong>size</strong> of the stack of this function call. Local variables come right after return address (ebp-4, ebp-8, etc). So what is the overall purpose of esp here? From what I understand, stack overflow occurs when we try to access an address smaller than that. </p>
  
  <p>The last thing is: when we say the top of the stack, are we referring to the lowest address (for x86).</p>
  
  <p>This is the picture I have in mind (growing downward)</p>
  
  <pre><code>[Parameter n          ]
  ...
  [Parameter 2          ]
  [Parameter 1          ]
  [Return Address       ]   0x002CF744
  [Previous EBP         ]   0x002CF740  (current ebp)
  [Local Variables      ]   
  -- ESP
  </code></pre>
  
  <p>Sorry for these long questions. But I really appreciate your help. </p>