Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- arrayDisplay macro Array, numOfElemnts
- push ebx
- mov ebx, offset Array
- i = 0
- while i LT numOfElemnts
- movsd xmm0, real8 ptr [ebx + i * type real8]
- sub esp, 8
- movsd real8 ptr [esp], xmm0
- push offset outputFormatString
- call printf_s
- add esp, 12
- i = i + 1
- endm
- pop ebx
- endm
- .686
- .xmm
- .model flat, c
- includelib libcmt.lib
- includelib libvcruntime.lib
- includelib libucrt.lib
- includelib legacy_stdio_definitions.lib
- extern printf_s : proc
- .data
- array real8 3.0, 5.0, 4.0, 8.0, 12.0, 4.50, 8.45, 8.10
- outputFormatString byte "%.2lf, ",0
- beforeSorting byte "The array before sorting = [", 0
- afterSorting byte "The array after sorting = [", 0
- closingBracket byte 8,8,"]", 13, 10, 0
- .code
- main proc
- push offset beforeSorting ; printf_s("The array before sorting = [")
- call printf_s
- add esp, 4
- arrayDisplay array, 8 ; a macro to display the array before sorting
- push offset closingBracket ; printf_s("]")
- call printf_s
- add esp, 4
- push sizeof array ; pushing the array size into the stack
- push offset array ; pushing the array starting address into the stack
- call selectionSort ; sorting the array
- add esp, 8 ; clearing the stack
- push offset afterSorting ; printf_s("The array after sorting = [")
- call printf_s
- add esp, 4
- arrayDisplay array, 8 ; a macro to display the array after sorting
- push offset closingBracket ; printf_s("]")
- call printf_s
- add esp, 4
- xor eax, eax
- ret
- main endp
- selectionSort proc, x: ptr dword, sizeOfX: dword
- pushad ; save all registers
- mov eax, x ; eax <------ offset of array x
- xor ebx, ebx ; ebx <------ k = 0
- xor ecx, ecx ; ecx <------ j = 0
- xor edx, edx ; edx <------ minpos = 0
- mov edi, sizeOfX ; edi <------ size - 2
- sub edi, type x * 2
- mov esi, sizeOfX ; esi <------ size - 1
- sub esi, type x
- Loop_k: ; for(k = 0; k <= size - 2; k++)
- cmp ebx, edi
- ja End_Loop_k
- mov edx, ebx ; minpos = k
- mov ecx, ebx ; j = k + 1
- add ecx, type real8
- Loop_j: ; for(j = k + 1; j <= size - 1; j++)
- cmp ecx, esi
- ja End_Loop_j
- movsd xmm0, real8 ptr [eax + ecx] ; xmm0 <---- x[j]
- movsd xmm1, real8 ptr [eax + edx] ; xmm1 <---- x[minpos]
- comisd xmm0, xmm1 ; if(x[j] < x[minpos])
- jae No_Swap
- mov edx, ecx ; minpos = j
- No_Swap:
- add ecx, type real8 ; j++
- jmp Loop_j
- End_Loop_j:
- movsd xmm0, real8 ptr [eax + ebx] ; x[minpos] <---- x[k]
- movsd xmm1, real8 ptr [eax + edx] ; x[k] <---- x[minpos]
- movsd real8 ptr [eax + edx], xmm0
- movsd real8 ptr [eax + ebx], xmm1
- add ebx, type real8 ; k++
- jmp Loop_k
- End_Loop_k:
- popad ; restore all registers
- ret
- selectionSort endp
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement