title: push local variables
body: <p>Alo</p>
  
  <p>After I have read about function and stack from <a href="http://en.wikibooks.org/wiki/X86_Disassembly/Functions_and_Stack_Frames" rel="noreferrer">http://en.wikibooks.org/wiki/X86_Disassembly/Functions_and_Stack_Frames</a> I have a question about local variables.</p>
  
  <p>Snapshot from the article:</p>
  
  <hr>
  
  <pre><code>push ebp     ; save the value of ebp
  mov ebp, esp ; ebp now points to the top of the stack
  sub esp, 12  ; space allocated on the stack for the local variables
  </code></pre>
  
  <p>This means local variables can be accessed by referencing ebp. Consider the following C code fragment and corresponding assembly code:</p>
  
  <pre><code>a = 10;
  b = 5;
  c = 2;
  
  mov [ebp -  4], 10  ; location of variable a
  mov [ebp -  8], 5   ; location of b
  mov [ebp - 12], 2   ; location of c
  </code></pre>
  
  <p>Remember that pushing basically does this:</p>
  
  <pre><code>sub esp, 4   ; "allocate" space for the new stack item
  mov [esp], X ; put new stack item value X in
  </code></pre>
  
  <p><hr>
  Why are local variables not push into the stack like this:</p>
  
  <pre><code>push 10
  push 5
  push 2
  </code></pre>
  
  <p>instead of </p>
  
  <pre><code>sub esp, 12
  
  mov [ebp -  4], 10  ; location of variable a
  mov [ebp -  8], 5   ; location of b
  mov [ebp - 12], 2   ; location of c
  </code></pre>