Subversion Repositories Kolibri OS

Rev

Rev 542 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ; Sorting bunch of dwords, count = ecx, locating at address = edx,
  2. ; comparison function at ebx
  3. ; Destroy content of eax, ecx, esi, edi
  4. sort:
  5.         jecxz   .done
  6.         mov     eax, ecx
  7.  
  8.     @@:
  9.         push    eax
  10.         call    .restore
  11.         pop     eax
  12.         dec     eax
  13.         jnz     @b
  14.  
  15.     @@:
  16.         cmp     ecx, 1
  17.         jz      .done
  18.         mov     esi, 1
  19.         mov     edi, ecx
  20.         call    .exchange
  21.         dec     ecx
  22.         mov     eax, 1
  23.         call    .restore
  24.         jmp     @b
  25.  
  26.     .done:
  27.         ret
  28.  
  29.     .exchange:
  30.         push    eax ecx
  31.         mov     eax, [edx+esi*4-4]
  32.         mov     ecx, [edx+edi*4-4]
  33.         mov     [edx+esi*4-4], ecx
  34.         mov     [edx+edi*4-4], eax
  35.         pop     ecx eax
  36.         ret
  37.  
  38.     .restore:
  39.         lea     esi, [eax+eax]
  40.         cmp     esi, ecx
  41.         ja      .donerr
  42.         push    esi
  43.         mov     esi, [edx+esi*4-4]
  44.         mov     edi, [edx+eax*4-4]
  45.         call    ebx
  46.         pop     esi
  47.         ja      .need_xchg
  48.         cmp     esi, ecx
  49.         jae     .donerr
  50.         push    esi
  51.         mov     esi, [edx+esi*4]
  52.         mov     edi, [edx+eax*4-4]
  53.         call    ebx
  54.         pop     esi
  55.         jbe     .donerr
  56.  
  57.     .need_xchg:
  58.         cmp     esi, ecx
  59.         jz      .do_xchg
  60.         push    esi
  61.         mov     edi, [edx+esi*4-4]
  62.         mov     esi, [edx+esi*4]
  63.         call    ebx
  64.         pop     esi
  65.         sbb     esi, -1
  66.  
  67.     .do_xchg:
  68.         mov     edi, eax
  69.         call    .exchange
  70.         mov     eax, esi
  71.         jmp     .restore
  72.  
  73.     .donerr:
  74.         ret
  75.  
  76. ; vim: ft=fasm tabstop=4
  77.  
  78.