title: Assembly language to C
body: <p>So I have the following assembly language code which I need to convert into C. I am confused on a few lines of the code.</p>
  
  <p>I understand that this is a <code>for</code> loop. I have added my comments on each line.</p>
  
  <p>I think the <code>for</code> loop goes like this</p>
  
  <pre><code>for (int i = 1; i &gt; 0; i &lt;&lt; what?) {
      //Calculate result
  }
  </code></pre>
  
  <p>What is the test condition? And how do I change it?</p>
  
  <p>Looking at the assembly code, what does the variable 'n' do?</p>
  
  <p>This is Intel x86 so the format is movl = source, dest</p>
  
  <pre><code>  movl 8(%ebp), %esi     //Get x
    movl 12(%ebp), %ebx    //Get n
    movl $-1, %edi         //This should be result
    movl $1, %edx          //The i of the loop
  .L2:
    movl %edx, %eax
    andl %esi, %eax
    xorl %eax, %edi        //result = result ^ (i &amp; x)
    movl %ebx, %ecx        //Why do we do this? As we never use $%ebx or %ecx again
    sall %cl, %edx         //Where did %cl come from?
    testl %edx, %edx       //Tests if i != what? - condition of the for loop
    jne .L2                //Loop again
    movl %edi, %eax        //Otherwise return result.
  </code></pre>