title: NASM - Selection sort implementation doesn't work
body: <p>The problem with segmentation fault has been solved, however there still remains a question of faulty algorithm itself which sorts numbers correctly but puts some of the biggest ones on the top of the file.
  For example numbers:</p>
  
  <pre>
   600196455
  1215535209
  1560271953
  1491899466
  2093817641
  3618330810
   519782898
  3779504611
  1656881276
   670566484
  </pre>
  
  <p>after sorting become:</p>
  
  <pre>3618330810
  3779504611
   519782898
   600196455
   670566484
  1215535209
  1491899466
  1560271953
  1656881276
  2093817641
  </pre>
  
  <hr>
  
  <p><strong>OLD</strong>
  I am trying to implement an assembler program to read <strong>32bit</strong> numbers from a file(maximum 32768 numbers), sort them using selection sort and save them to a specified file.
  So far I have successfully written parts that read and write data however there is an error in my sorting algorithm which results in a segmentation fault.</p>
  
  <p>I am using 32bit Fedora, <strong>i386</strong> processor, <strong>NASM</strong> assembler. </p>
  
  <p>Thanks</p>
  
  <p>My code looks as following</p>
  
  <pre><code>section .text
  global _start
  
  _start:
      ; read from input
      mov eax, 3
      mov ebx, 0
      mov ecx, array
      mov edx, 4*32768
      int 80h
  
      mov [fileLength], eax
      shr eax, 2
      mov [numbersCount], eax
      mov [numbersCount1], eax
      dec dword [numbersCount1]
  
      ; sort
      mov ebp, array
      xor ecx, ecx
      for1:
              mov dword [min], ecx
              xor edx, edx
              mov edx, ecx
              inc edx
              for2:
                      xor esi, esi
                      mov esi, dword [min]
                      mov eax, dword [ebp+4*edx]
                      mov ebx, dword [ebp+4*esi]
                      cmp eax, ebx
                      jge if1
                      mov dword [min], edx
                      if1:
                              inc edx
                              cmp edx, [numbersCount]
                              jl for2
                      xor ebx, ebx
                      xor edx, edx
                      mov ebx, dword [min]
  
                      mov eax, dword [ebp+4*ebx]
                      mov edx, dword [ebp+4*ecx]
                      mov dword [ebp+4*ebx], edx
                      mov dword [ebp+4*ecx], eax
  
                      inc ecx
                      cmp ecx, [numbersCount1]
                      jl for1
                      ret
      ; write to output
      mov eax, 4
      mov ebx, 1
      mov ecx, array
      mov edx, [fileLength]
      int 80h
  
      ; exit to linux
      mov     eax,1
      mov     ebx,0
      int     80h
  
  ; initialized data section
  ; use directives DB (byte), DW (word), DD (doubleword), DQ (quadword)
  section .data
  
  ; uninitialized data section
  ; use directives RESB (byte), RESW (word), RESD (doubleword), RESQ (quadword)
  section .bss
      fileLength resd 1
      numbersCount resd 1
      numbersCount1 resd 1
      min resd 1
      array resd 32768
      item resd 1
  </code></pre>