title: Assembly Function Integer Local Variable
body: <p>I'm trying to learn basic assembly. I wrote a simple program in C to translate to assembly:</p>
  
  <pre><code>void myFunc(int x, int y) {
      int z;
  }
  
  int main() {
      myFunc(20, 10);
      return 0;
  }
  </code></pre>
  
  <p>This is what I thought the correct translation of the function would be:</p>
  
  <pre><code>.text
  .globl _start
  .type myFunc, @function
  
  myFunc:
      pushl %ebp        #Push old ebp register on to stack
      movl %esp, %ebp   #Move esp into ebp so we can reference vars
      sub $4, %esp      #Subtract 4 bytes from esp to make room for 'z' var
      movl $2, -4(%ebp) #Move value 2 into 'z'
      movl %ebp, %esp   #Restore esp
      popl %ebp         #Set ebp to 0?
      ret               #Restore eip and jump to next instruction
  
  _start:
      pushl $10        #Push 10 onto stack for 'y' var
      pushl $20        #Push 20 onto stack for 'x' var
      call myFunc      #Jump to myFunc (this pushes ret onto stack)
      add $8, %esp     #Restore esp to where it was before
  
      movl $1, %eax    #Exit syscall
      movl $0, %ebx    #Return 0
      int $0x80        #Interrupt
  </code></pre>
  
  <p>Just to double check it I ran it in gdb and was confused by the results:</p>
  
  <pre><code>(gdb) disas myFunc
  Dump of assembler code for function myFunc:
  0x08048374 &lt;myFunc+0&gt;:  push   ebp
  0x08048375 &lt;myFunc+1&gt;:  mov    ebp,esp
  0x08048377 &lt;myFunc+3&gt;:  sub    esp,0x10
  0x0804837a &lt;myFunc+6&gt;:  leave
  0x0804837b &lt;myFunc+7&gt;:  ret    
  End of assembler dump.
  </code></pre>
  
  <p>Why at 0x08048377 did gcc subtract 0x10 (16 bytes) from the stack when an integer is 4 bytes in length?</p>
  
  <p>Also, is the leave instruction equivalent to the following?</p>
  
  <pre><code>    movl %ebp, %esp   #Restore esp
      popl %ebp         #Set ebp to 0?
  </code></pre>
  
  <p>Using:</p>
  
  <pre><code>gcc version 4.3.2 (Debian 4.3.2-1.1)
  GNU gdb 6.8-debian
  </code></pre>