global _start section .bss buffer resb 20 ; Reserve 20 bytes for converting numbers to ascii section .data array dd 5,4,3,2,1 len equ (($ - array) / 4) min dd 0 max dd 0 avg dd 0 ; Some strings to aid in printing unsorted_str db "Unsorted values: " unsorted_str_len equ ($ - unsorted_str) sorted_str db "Sorted values: " sorted_str_len equ ($ - sorted_str) avg_str db "Average: " avg_str_len equ ($ - avg_str) min_str db "Min: " min_str_len equ ($ - min_str) max_str db "Max: " max_str_len equ ($ - max_str) newline_str db 0x0a comma_str db "," section .text _start: ; Print the unsorted array mov eax, unsorted_str mov ebx, unsorted_str_len call print call print_array mov eax, newline_str mov ebx, 1 call print ; Sort call bubblesort ; Print the sorted array mov eax, sorted_str mov ebx, sorted_str_len call print call print_array mov eax, newline_str mov ebx, 1 call print ; Min call min_fn mov eax, min_str mov ebx, min_str_len call print mov eax, [min] call print_uint mov eax, newline_str mov ebx, 1 call print ; Max call max_fn mov eax, max_str mov ebx, max_str_len call print mov eax, [max] call print_uint mov eax, newline_str mov ebx, 1 call print ; Average call avg_fn mov eax, avg_str mov ebx, avg_str_len call print mov eax, [avg] call print_uint mov eax, newline_str mov ebx, 1 call print call exit ; Some c code to reference ; for (int i = 0; i < len; i++) { ; bool swapped = false; ; for (int j = 0; j < n - i - 1; j++) { ; if (arr[j] > arr[j + 1]) { ; int temp = arr[j]; ; arr[j] = arr[j + 1]; ; arr[j + 1] = temp; ; swapped = true; ; } ; if (!swapped) break; ; } ; } ; eax = array[j] ; ebx = array[j+1] ; ecx = outer counter ; edx = swap flag ; esi = inner counter ; bubblesort: mov ecx, len dec ecx cmp ecx, 1 jle done outer_loop: xor esi, esi ; clear inner counter xor edx, edx ; clear swap flag inner_loop: mov eax, [array + esi * 4] ; array[j] mov ebx, [array + esi * 4 + 4] ; array[j + 1] cmp eax, ebx jle no_swap ; if array[j] <= array[j + 1], skip the swap ; Swap the values mov [array + esi * 4], ebx ; Set array[j] to array[j+1] mov [array + esi * 4 + 4], eax ; Set array[j + 1] to array[j] mov edx, 1 ; Set the swap flag no_swap: inc esi ; j++ cmp esi, ecx jl inner_loop ; Continue inner loop if j < ecx test edx, edx jz done ; Jump to done if nothing was swapped loop outer_loop ; Loop done: ret min_fn: mov eax, [array] mov [min], eax ret max_fn: mov eax, [array + len * 4 - 4] mov [max], eax ret avg_fn: xor eax, eax xor esi, esi avg_loop: add eax, [array + esi * 4] inc esi cmp esi, len jl avg_loop xor edx, edx mov ecx, len div ecx mov [avg], eax ret ; Most of the stuff down here is just ; me being fancy with it. Im writing this ; for i386 linux so its all syscalls for ; basic stuff ; Exit the program cleanly exit: mov eax, 1 ; syscall code for exiting xor ebx, ebx ; Our return code int 0x80 ; Call the system handler ; Taking a uint from eax, this will convert ; it to an ascii string and then print to ; stdout print_uint: mov ecx, buffer + 11 ; Point to the end of the buffer mov ebx, 10 ; Set the divisor convert_loop: xor edx, edx ; div divides edx:eax, clear edx div ebx ; divide! add dl, '0' ; convert the remainder to ASCII dec ecx ; Mov buffer ptr back mov [ecx], dl ; Store the character test eax, eax, ; Check if quotient is 0 jnz convert_loop ; Jmp mov edx, buffer + 12 ; Get the length of the string sub edx, ecx ; edx is the length mov eax, 4 ; sys_write call code mov ebx, 1 ; stdout file descriptor int 0x80 ; syscall int ret ; Taking an ptr to an ascii string on eax ; and the length on ebx, this will print ; it to stdout print: mov ecx, eax ; Move eax (msg ptr) to ecx mov edx, ebx ; Move ebx (len) to edx mov eax, 4 ; set sys_write call mov ebx, 1 ; stdout file descriptor int 0x80 ; Call the system handler ret ; Loops through the array and prints each ; value to stdout print_array: xor esi, esi print_loop: mov eax, [array + esi * 4] call print_uint mov eax, comma_str mov ebx, 1 call print inc esi cmp esi, len jl print_loop ret