title: how to sort an array in assembly x86
body: <p>I have trouble with the sorting an array in assembly. In order to reorder an array with numbers, I have two functions. The first one is finding the index of the smallest integer, and the second one is to swap the original array. I don't know why my code doesn't work. Appreciate a lot!</p>
  
  <pre><code>__asm
  {
      // YOUR CODE STARTS HERE
          push ebp
          mov ebp, esp
  
          push edi
          push esi
          push ebx
  
          mov eax, [ebp + 8] // array
          mov ebx, [ebp + 12] // i 
          mov ecx, [ebp + 16] //j
          mov edx, ebx //index 
  BEGIN_LOOP :
          cmp ebx, ecx
          jge END_LOOP
          mov esi, [eax + 4 * ebx]
          mov edi, [eax + 4 * edx]
          cmp esi, edi
          jge CONTINUE
          mov edx, ebx
  CONTINUE:
      inc ebx
      jmp BEGIN_LOOP
  END_LOOP:
      mov eax, edx
      pop ebx
      pop esi
      pop edi
  
      mov esp, ebp
      pop ebp
      // YOUR CODE ENDS HERE
      ret
  }
  </code></pre>
  
  <p>The other part of the code:</p>
  
  <pre><code>void selectionSort (int integer_array[], int array_size)
  {
      // YOUR CODE STARTS HERE
          mov eax, [ebp + 12] // array_size
          mov esi, [ebp + 8] // integer_array
          mov edi, 0 // j 
          sub eax, 1
      FORLOOP1: // for (j = 0; j &lt; array_size-1; j++)
          cmp eax, edi
          jl FORLOOP1_END
          mov ebx, [ebp + 12]
          push ebx
          push edi
          push esi
          call findMinIndex
          add esp, 12
          mov ebx, edx
          cmp edi, ebx
          je LABEL
          mov edx, [esi + 4 * ebx]
          mov ecx,[esi + 4 * edi]
          xchg edx, ecx
          mov[esi + 4 * ebx], edx
          mov[esi + 4 * edi], ecx
  
      LABEL :
          inc edi
          jmp FORLOOP1
  
      FORLOOP1_END :
          mov[ebp + 8], esi
  
      // YOUR CODE ENDS HERE
  }
  </code></pre>