Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
3675 GerdtR 1
; Sorting bunch of dwords, count = ecx, locating at address = edx,
2
; comparison function at ebx
3
; Destroy content of eax, ecx, esi, edi
542 diamond 4
sort:
5
        jecxz   .done
6
        mov     eax, ecx
3675 GerdtR 7
 
8
    @@:
542 diamond 9
        push    eax
10
        call    .restore
11
        pop     eax
12
        dec     eax
13
        jnz     @b
3675 GerdtR 14
 
15
    @@:
542 diamond 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
3675 GerdtR 25
 
26
    .done:
542 diamond 27
        ret
28
 
3675 GerdtR 29
    .exchange:
542 diamond 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
 
3675 GerdtR 38
    .restore:
542 diamond 39
        lea     esi, [eax+eax]
40
        cmp     esi, ecx
3675 GerdtR 41
        ja      .donerr
542 diamond 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
3675 GerdtR 49
        jae     .donerr
542 diamond 50
        push    esi
51
        mov     esi, [edx+esi*4]
52
        mov     edi, [edx+eax*4-4]
53
        call    ebx
54
        pop     esi
3675 GerdtR 55
        jbe     .donerr
56
 
57
    .need_xchg:
542 diamond 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
3675 GerdtR 66
 
67
    .do_xchg:
542 diamond 68
        mov     edi, eax
69
        call    .exchange
70
        mov     eax, esi
71
        jmp     .restore
3675 GerdtR 72
 
73
    .donerr:
542 diamond 74
        ret
3675 GerdtR 75
 
76
; vim: ft=fasm tabstop=4
77