title: Assembly (MASM) quickSort
body: <p>So I have been working on this procedure for a few days now. It seems to works only sometimes, the rest of the time I get a stack overflow error when I run it in VS. 
  My program generates random numbers in a range 100 to 999 in to an array, the size of which is inputed by user (max size 200), which is what I declared my array size as. After which I use my quickSort proc, here is the code</p>
  
  <pre><code>;-------------------------------------------------------
  sortList PROC
  ; Sets up call to quickSort by calculating size of array,
  ; then passing high, low, and @array as arugments.
  ; Receives: @array: PTR DWORD   ; reference to array
  ;           req:DWORD           ; value of the requested number
  ; Returns:  nothing
  ;-------------------------------------------------------
  enter   0,0                 
  
  mov     eax, [ebp+12]   ; value of request
  mov     ecx, 4          ; size of each element in the array
  mul     ecx             ; multiplied to get sizeof the array
  mov     ebx, eax        ; ebx = size of array
  sub     ebx, 4          ; sub 4 for starting address last element
  mov     eax, 0          ; zero eax for beginning element
  mov     esi, [ebp+8]    ; @array
  
  push    ebx             ; high
  push    eax             ; low
  push    esi             ; @array
  call    quickSort
  
  pop     ebx             ; restore ebx
  pop     eax             ; restore eax
  pop     esi             ; restore esi
  leave
  ret 8
  sortList ENDP
  
  ;-------------------------------------------------------
  quickSort PROC
  ; Uses recursion to sort the array.
  ; Receives: @arr: PTR DWORD     ; reference to array
  ;           LEFT:DWORD          ; value of beg index
  ;           RIGHT:DWORD         ; value of end index
  ; Returns:  nothing
  ; Adapted from: Algorithm implemented in C
  ; http://www.algolist.net/Algorithms/Sorting/Quicksort
  ;-------------------------------------------------------
  LOCAL   right:DWORD,        ; high value of array
          left:DWORD          ; low value of array
  
  mov     eax, [ebp+12]   ; Value of low into eax
  mov     left, eax       ; left = low
  mov     ebx, [ebp+16]   ; Value of high into ebx
  mov     right, ebx      ; right = high
  mov     esi, [ebp+8]    ; @array
  
  mov     edi, [esi]      ; edi = pivot
  mov     eax, left       ; eax = i
  mov     ebx, right      ; ebx = j
  
  mainLoop:
  cmp     eax, ebx        ; Loop while i &lt;= j
  jg      endMain
  iCheck:
  cmp     [esi+eax], edi  ; Loop while array[i] &gt; pivot
  jle     endiCheck
  add     eax, 4
  jmp     iCheck
  endiCheck:
  
  jCheck:
  cmp     [esi+ebx], edi  ; Loop while array[j] &lt; pivot
  jge     endjCheck
  sub     ebx, 4          ; move to array[j-1]
  jmp     jCheck
  endjCheck:
  
  cmp     eax, ebx        ; If i &lt;= j, swap the places in the array
  jg      endSwap
  ; Using stack properties to perform swap
  push    [esi+eax]
  push    [esi+ebx]
  pop     [esi+eax]
  pop     [esi+ebx]
  
  add     eax, 4          ; increment i
  sub     ebx, 4          ; decrement j
  endSwap:
  jmp     mainLoop
  endMain:
  mov     ecx, right          ; restore right index into ecx
  mov     edx, left           ; restore left index into edx
  Lrecursion:
  cmp     edx, ebx        ; Compare left to j
  jge     Rrecursion      ; If left &lt; j, then call quickSort again
  push    ebx             ; high value of left half
  push    edx             ; low value of left half 
  push    esi             ; @array 
  call    quickSort
  
  Rrecursion:
  cmp     eax, ecx        ; Compare i to right
  jge     theEnd          ; If i &lt; right, call quickSort
  push    ecx             ; high value of right half
  push    eax             ; low value of right half
  push    esi             ; @array
  call    quickSort
  
  theEnd:
  ret     12
  
  quickSort ENDP
  </code></pre>
  
  <p>After stepping through with the breakpoints in the debugger I think there is a logic error I am missing. I input an array of three. The values generated were 472 108 988. My code correctly put 988 in place, but once it got to sorting the last two it was not able to resolve.
  A pointer in the right direction would be much appreciated. I hope I came across clear. Thanks in advance for any advice!</p>