%include "MY_IO.h" extern GET_RANDOM extern SEED_RANDOM extern RANDOM_SEED ; constants NUMBER_COUNT equ 20 MAX_VALUE equ 1000 segment .data ArraySize dq 0 SPACE_CHAR db " ",0 segment .bss Array resd NUMBER_COUNT segment .text global _start ; rdi is the address of the array ; rsi is the address of the size of the array ; both are pass by pointer ; ; r15 will be the lcv ; r14 will be the base address of the array ; r13 will be the address of the size ; r12 will be the offset in the array FillArray: push rbp mov rbp, rsp push r15 push r14 push r13 push r12 mov r14, rdi mov r13, rsi ; seed the random number generator mov rdi, 1 call RANDOM_SEED ; for r15 = 0; r15 < NUMBER_COUNT ; ++r15 mov r15, 0 mov r12, 0 FillArray_top: cmp r15, NUMBER_COUNT jz FillArray_done ; rax = rand() call GET_RANDOM ; A[r12] = rax % MAX_VALUE mov edx, 0 mov ecx , MAX_VALUE div ecx mov [r14+r12], edx inc r15 ; dealing with 32 bit words, so increment by 4 add r12, 4 jmp FillArray_top FillArray_done: ; ArraySize = NUMBER_COUNT mov [r13], r15 pop r12 pop r13 pop r14 pop r15 pop rbp ret ; rdi will hold the base address of the array ; rsi will hold the length of the array ; ; r14 = base address of the array ; for(r15 =0; r15 != r13; ++r15) { ; cout << memory[r14] << " " ; r14 += 4 ; } ; cout << endl ; ; r15 will be the lcv ; r14 for the array address + offset ; r13 for the size PrintArray: push rbp mov rbp, rsp push r15 push r14 push r13 mov r15, 0 mov r14, rdi mov r13, rsi PrintArray_top: cmp r15, r13 jz PrintArray_done mov edi, [r14] call PRINT_INT mov rdi, SPACE_CHAR mov rsi, 2 call PRINT_STRING add r15, 1 add r14, 4 jmp PrintArray_top PrintArray_done: call PRINT_NEWLINE ; exit pop r13 pop r14 pop r15 pop rbp ret ; rdi will be the base address of the array ; rsi will be the size of the array ; ; selection sort ; for pos = 0; pos < size; ++pos ; smallPos = pos ; for j = pos+1; j < size; ++j ; if A[j] < A[smallPos] ; smallPos = j ; swap(A[pos], A[smallPos] ; ; r15 will be the lcv (pos) ; r14 will be the arraypos for pos ; r13 will be lcv (j) ; r12 will be array pos for j ; r11 will be smallpos ; r10 is a temp variabel (swap) ; r9 is a temp variable (swaps and comparison) SelectionSort: push rbp mov rbp, rsp push r15 push r14 push r13 push r12 ; no need to save rsi or rdi, I will call nothing. mov r15, 0 ; lcv i = 0 mov r14, rdi ; r14 points at the current position ; for pos = 0; pos < size; ++pos SelectionSort_loop_1_top: cmp r15, rsi jz SelectionSort_done mov r11, r14 ; the first position is now the smallest ; for j = pos+1; mov r13, r15 inc r13 mov r12, r14 ; r12 is the array position for j add r12, 4 ; for j = pos+1; j< size; ++j SelectionSort_loop_2_top: cmp r13, rsi jz SelectionSort_loop_2_done ; cmp is not allowed to compare two memory locations mov r9, [r11] cmp r9d, dword[r12] jl SelectionSort_end_if mov r11, r12 ; smallPos = jPos SelectionSort_end_if: inc r13 add r12, 4 jmp SelectionSort_loop_2_top SelectionSort_loop_2_done: ; if smallpos != pos cmp r11, r14 je SelectionSort_second_if ; sawp (A[smallpos], A[pos]) mov r9d, dword [r14] mov r10d, dword[r11] mov [r14], r10d mov [r11], r9d SelectionSort_second_if: inc r15 add r14, 4 jmp SelectionSort_loop_1_top SelectionSort_done: ; exit pop r12 pop r13 pop r14 pop r15 pop rbp ret _start: ; fill the array with NUMBER_COUNT values mov rdi, Array mov rsi, ArraySize call FillArray ; print the array mov rdi, Array mov rsi, [ArraySize] call PrintArray ; sort the array mov rdi, Array mov rsi, [ArraySize] call SelectionSort ; print the array mov rdi, Array mov rsi, [ArraySize] call PrintArray ; exit mov eax, SYS_EXIT mov edx, SUCCESS syscall