title: Wrong value when swapping array values in x86 assembly
body: <p>I'm working on quicksort in x86 assembly and I need to swap two elements of an array A[pivot] and A[j], but I can't even assign a value to an array index let alone swap the elements.</p>
  
  <p>Array is assigned as such:</p>
  
  <pre><code>A:      .long   2,1,8,6,12
  </code></pre>
  
  <p>My original scheme of swapping didn't work at all so I reduced it to this to understand where my problem is. I've tried numerous ways to get the correct results, but all either result in the wrong value or a segmentation error</p>
  
  <pre><code>    movl    A(,%ebx,4), %eax            #eax = A[pivot]
      movl    A(,%edi,4), %edx            #edx = A[j]
  
  
                                          #ebx = pivot = 0
                                          #edi = j = 1
  
      pushl   %eax
      pushl   $test7                      #"A[pivot] = %d"
      call    printf
      addl    $8, %esp                    # A[0] = 2 
  
      pushl   %edx
      pushl   $test8                      #"A[j] = %d"
      call    printf
      addl    $8, %esp                   #A[1] = 1
  </code></pre>
  
  <p>This snippet returns:</p>
  
  <pre><code>A[pivot]         = 2 
  A[j]             = -143535296 
  </code></pre>
  
  <p>A[pivot] = A[0] = 2, so that is correct, but
  A[j] = A[1] = 1</p>
  
  <p>Is this the correct way to reference array elements when %ebx and %edi are two array indices to view their contents or change their values.</p>
  
  <p>Can't figure out what I'm doing wrong, any help would be appreciated.</p>
  
  <p>edit: Also, if I use A(,[index],4) as a printf argument it DOES display the right values. </p>
  
  <p>edit1: I realize why my printf statements were incorrect, I changed to the code and it returned  what seem to be the right memory addresses. addr[A] = 134513652 and addr[A+1] = 134513656. My original problem of changing the values of an array still exists though, I continue to get a segmentation fault when performing this:</p>
  
  <pre><code>    leal    A(,%ebx,4), %ecx        # ecx = addr[A[0]]          
      movl    A(,%edi,4), %edx        # edx = A[1]
      movl    %edx, (%ecx)            # (ecx) = edx
  </code></pre>