title: .NET functions disassembled
body: <p>When disassembling .NET functions, I notice that they all start with a similair pattern.
  What does this initial code do?</p>
  
  <p>This code appear before the actual code for what the function is supposed to do.
  Is it some sort of parameter count verification?</p>
  
  <p>func1</p>
  
  <pre><code>private static void Foo(int i)
  {
     Console.WriteLine("hello");
  }
  
  00000000  push        ebp 
  00000001  mov         ebp,esp 
  00000003  push        eax 
  00000004  mov         dword ptr [ebp-4],ecx 
  00000007  cmp         dword ptr ds:[005C14A4h],0 
  0000000e  je          00000015 
  00000010  call        65E0367F 
  //the console writleline code follows here and is not part of the question
  </code></pre>
  
  <p>func2</p>
  
  <pre><code>static private void Bar()
  {
     for (int i = 0; i &lt; 1000; i++)
     {
        Foo(i);
     }
  }
  
  00000000  push        ebp 
  00000001  mov         ebp,esp 
  00000003  push        eax 
  00000004  cmp         dword ptr ds:[006914A4h],0 
  0000000b  je          00000012 
  0000000d  call        65CC36CF 
  // the for loop code follows here
  </code></pre>
  
  <p>func3</p>
  
  <pre><code>private static void Foo()
  {
     Console.WriteLine("hello");
  }
  
  00000000  push        ebp 
  00000001  mov         ebp,esp 
  00000003  cmp         dword ptr ds:[005614A4h],0 
  0000000a  je          00000011 
  0000000c  call        65E3367F 
  </code></pre>
  
  <p>[Edit]
  So is this a correct description of it?</p>
  
  <pre><code>//fix stackframe
  00000000  push        ebp 
  00000001  mov         ebp,esp 
  //store eax so it can be used locally
  00000003  push        eax 
  //ensure static ctor have been called
  00000004  cmp         dword ptr ds:[006914A4h],0 
  //it has been called, ignore it
  0000000b  je          00000012
  //it hasn't been called, call it now 
  0000000d  call        65CC36CF 
  </code></pre>
  
  <p>or?</p>