Skip to content

Instantly share code, notes, and snippets.

@Enelar
Created January 24, 2015 13:09
Show Gist options
  • Save Enelar/12bc90278f8b7e17fcaf to your computer and use it in GitHub Desktop.
Save Enelar/12bc90278f8b7e17fcaf to your computer and use it in GitHub Desktop.
One friend asked help with home work. Its crappy snippet perform selection sort.
.686
.model flat
.stack
data segment "data"
data ends
.CODE
; stack:
; pointer to compare
; pointer to array end
; pointer to array start
; EDX - pointer to compare
; EBX - array end
; ECX - swap candidate
; ESI - array current position
; EDI - array current base
sort_kb3 PROC
cmp ESI, EBX ; until not pointing at end of array
jne begin ; begin
ret
begin:
mov EDI, ESI
mov ECX, ESI
loop_begin:
cmp EDI, EBX
je loop_end
;comparing
; save registers
push EDX
push EBX
; prepare call
push EDI
push ECX
call EDX
pop ECX
pop EDI
pop EBX
pop EDX
cmp EAX, 1
jne not_minimal
mov ECX, EDI
not_minimal:
add EDI, 4
jmp loop_begin
loop_end:
; swap to minimal
mov EAX, dword ptr [ESI]
mov EDI, dword ptr[ECX]
mov dword ptr[ESI], EDI
mov dword ptr[ECX], EAX
add ESI, 4 ; move base, first element sorted already
jmp sort_kb3 ; restart snippet
sort_kb3 ENDP
; (pointer to struct, pointer to compate)
_sort_kb3 PROC
push EBP
mov EBP, ESP
mov EAX, [ESP+8] ; pointer to struct
mov EDX, [ESP+12] ; pointer to compate
push ESI
push EDI
mov ESI, dword ptr [EAX] ; pointer to array
mov ECX, dword ptr [EAX+4] ; pointer to size
mov EAX, ESI
shl ECX, 2 ; multiptly to int size
add EAX, ECX
mov EBX, EAX ; set array end
call sort_kb3
pop EDI
pop ESI
mov ESP, EBP
pop EBP
ret
_sort_kb3 ENDP
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment