; my quick sort program!
;
; use SASM under linux.
%include "io.inc"
section .data
dat db 9,13,8,6,8,22,-11,1,-4,9,7,2,-7,-5,4,2;
datlen equ $-dat
section .text
global CMAIN
CMAIN:
mov ebp, esp; for correct debugging
;write your code here
xor eax, eax
mov eax, dat
mov ebx, dat+ datlen
dec ebx
call quicksort
mov eax, dat
mov ebx, datlen
loopshow:
mov cl,byte[eax]
PRINT_DEC 1, cl
PRINT_STRING ","
inc eax
dec ebx
cmp ebx,0
jz loopshowout
jmp loopshow
loopshowout:
ret
quicksort: ; split in half.. datarange:eax-ebx.
cmp eax, ebx-1
jge nothing
jmp startpos
nothing:
ret
startpos:
push ebx
push eax
;eax = i
;ebx = j
mov ecx, eax;
inc ecx;
loop:
mov dh, byte[eax]
mov dl, byte[ecx]
cmp dh, dl
;cmp byte[eax], byte[ecx]
je equal
jl case1
jg case2
equal:
inc ecx
jmp endselect
case1:
mov dl, byte[ecx]
mov dh, byte[ebx]
mov byte[ebx], dl
mov byte[ecx], dh
dec ebx
jmp endselect
case2:
mov dl, byte[ecx]
mov dh, byte[eax]
mov byte[eax], dl
mov byte[ecx], dh
inc eax
inc ecx
jmp endselect
endselect:
mov edx,ebx
inc edx
cmp ecx, edx
je loopout
jmp loop
loopout:
quickpart1: ; orig_eax ~ eax -1
mov ebx, eax
dec ebx
pop eax
call quicksort
quickpart2: ; ebx ~ orig_ebx
mov eax, ecx
pop ebx
call quicksort
ret