%include "asm_io.inc"
segment .data
;
; initialized data is put in the data segment here
;
array: dd 3, 1, 5, 7, 2, 8, 4, 9, 6, 10
size: dd 10
string1 db "Origin : ",0x00
string2 db "Sorted : ",0x00
segment .text
global main
main:
enter 0,0 ; setup stack frame
pusha
mov eax, string1
call print_string
mov ecx, 0
repeat1:
mov eax, array
mov ebx, ecx
shl ebx, 2
mov eax, [eax+ebx]
call print_int
inc ecx
cmp ecx, [size]
je endrepeat1
mov eax, ','
call print_char
mov eax, ' '
call print_char
jmp repeat1
endrepeat1:
call print_nl
mov eax, [size]
dec eax
push dword eax
push dword 0
call quicksort
add esp, 8
mov eax, string2
call print_string
mov ecx, 0
repeat2:
mov eax, array
mov ebx, ecx
shl ebx, 2
mov eax, [eax+ebx]
call print_int
inc ecx
cmp ecx, [size]
je endrepeat2
mov eax, ','
call print_char
mov eax, ' '
call print_char
jmp repeat2
endrepeat2:
call print_nl
;
; code is put in the text segment. Do not modify the code before
; or after this comment.
;
popa
mov eax, 0 ; return value
leave ; leave stack frame
ret
global quicksort
quicksort:
push ebp
mov ebp, esp
mov eax, [ebp+8]
cmp eax, [ebp+12]
jge return
push eax ;first = ebp-4
mov ebx, [ebp+12]
add eax, ebx
shr eax, 1
push eax ;middle = ebp-8
push ebx ;last = ebp-12
sub esp, 16 ;for 4 dword local variable
rpt:
mov eax, [ebp-4]
cmp eax, [ebp-12]
jge endrpt
mov dword eax, array
mov ebx, [ebp-4]
shl ebx, 2
mov dword eax, [eax+ebx]
mov [ebp-16], eax
mov dword eax, array
mov ebx, [ebp-8]
shl ebx, 2
mov dword eax, [eax+ebx]
mov [ebp-20], eax
mov dword eax, array
mov ebx, [ebp-12]
shl ebx, 2
mov dword eax, [eax+ebx]
mov [ebp-24], eax
mov eax, [ebp-16]
cmp eax, [ebp-20]
jle endif1
mov ecx, [ebp-12]
while1:
mov eax, [ebp-24]
cmp eax, [ebp-20]
jle endwhile1
dec ecx
mov [ebp-12], ecx
mov eax, array
mov ebx, ecx
shl ebx, 2
mov eax, [eax+ebx]
mov [ebp-24], eax
jmp while1
endwhile1:
mov eax, [ebp-12]
cmp eax, [ebp-8]
jne endif2
mov eax, [ebp-4]
mov [ebp-8], eax
endif2:
mov dword eax, array
mov ebx, [ebp-4]
shl ebx, 2
mov edx, [ebp-24]
mov dword [eax+ebx], edx
mov ebx, [ebp-12]
shl ebx, 2
mov edx, [ebp-16]
mov dword [eax+ebx], edx
mov eax, [ebp-16]
mov [ebp-28], eax
mov eax, [ebp-24]
mov [ebp-16], eax
mov eax, [ebp-28]
mov [ebp-24], eax
endif1:
mov eax, [ebp-4]
cmp eax, [ebp-8]
jge endif3
mov eax, [ebp-4]
inc eax
mov [ebp-4], eax
endif3:
mov eax, [ebp-24]
cmp eax, [ebp-20]
jge endif4
mov ecx, [ebp-4]
while2:
mov eax, [ebp-16]
cmp eax, [ebp-20]
jge endwhile2
inc ecx
mov [ebp-4], ecx
mov eax, array
mov ebx, ecx
shl ebx, 2
mov eax, [eax+ebx]
mov [ebp-16], eax
jmp while2
endwhile2:
mov eax, [ebp-4]
cmp eax, [ebp-8]
jne endif5
mov eax, [ebp-12]
mov [ebp-8], eax
endif5:
mov dword eax, array
mov ebx, [ebp-4]
shl ebx, 2
mov edx, [ebp-24]
mov dword [eax+ebx], edx
mov ebx, [ebp-12]
shl ebx, 2
mov edx, [ebp-16]
mov dword [eax+ebx], edx
mov eax, [ebp-16]
mov [ebp-28], eax
mov eax, [ebp-24]
mov [ebp-16], eax
mov eax, [ebp-28]
mov [ebp-24], eax
endif4:
;mov eax, [middle]
mov eax, [ebp-8]
;cmp eax, [last]
cmp eax, [ebp-12]
jge endif6
mov eax, [ebp-12]
dec eax
mov [ebp-12], eax
endif6:
jmp rpt
endrpt:
push dword [ebp-8]
push dword [ebp+8]
call quicksort
add esp, 8
mov eax, [ebp-8]
inc eax
push dword [ebp+12]
push dword eax
call quicksort
add esp, 8
return:
mov esp, ebp
pop ebp
ret