title: Reading IA32 Assembly Code pertaining to using double arrays
body: <p>This is the exact question:</p>
  
  <blockquote>
    <p>The following Code transposes the elements of an M x M array, where M
    is a constant defined by #define:</p>
  </blockquote>
  
  <pre><code>void transpose(Marray_t A) {
      int i, j;
      for (i = 0; i &lt; M; i++)
          for (j = 0; j &lt; i; j++) {
             int t = A[i][j];
             A[i][j] = A[j][i];
             A[j][i] = t;
         } 
  }
  </code></pre>
  
  <blockquote>
    <p>When compiled with optimization level -02, GCC generates the following
    code for the inner loop of the function: </p>
  </blockquote>
  
  <pre><code>1. .L3 
  2. movl (%ebx),%eax 
  3. movl (%esi,%ecx,4),%edx 
  4. movl %eax, (%esi,%ecx,4) 
  5. addl $1, %ecx 
  6. movl %edx, (%ebx) 
  7. addl $52,%ebx 
  8. cmpl %edi,%ecx 
  9. jl .L3
  </code></pre>
  
  <blockquote>
    <p>A. What is the value of M? </p>
    
    <p>B. What registers hold program values i and
    j?  </p>
    
    <p>C. Write a C code version of transpose that makes use of the
    optimizations that occur in this loop. Use the parameter M in your
    code rather than numeric constant.</p>
  </blockquote>
  
  <p>So in my attempts to understand this, I notice that it is multiplying by 4, which means it stores types of 4 bytes (maybe an int or a pointer). Then, it increments i by $52 (I assume i) since it's a larger value, thus going to the next array) and $52 / 4 is 13. So I would guess that M = 13. Wrong? </p>
  
  <p>For B, I would guess that %ebx contains i and %ecx contains i. </p>
  
  <p>For C, I'm not exactly sure because I don't completely understand the loop presented. Let me try to understand by line number and tell me where I'm wrong. 1. is the beginning of the loop label obviously. 2. moves presumably the value of i into %eax. Then 3. has A[i][j] being stored into t. But... I really don't understand it. :/ Help?? </p>