|
| 1 | +.intel_syntax noprefix |
| 2 | + |
| 3 | +.section .rodata |
| 4 | + array: |
| 5 | + .align 16 |
| 6 | + .int 1, 45, 756, 4569, 56, 3, 8, 5, -10, -4 |
| 7 | + .equ array_len, (.-array) / 4 |
| 8 | + array_fmt: .string "%d " |
| 9 | + lf: .string "\n" |
| 10 | + unsorted: .string "Unsorted array: " |
| 11 | + sorted: .string "Sorted array: " |
| 12 | + |
| 13 | +.section .text |
| 14 | + .global main |
| 15 | + .extern printf |
| 16 | + |
| 17 | +print_array: |
| 18 | + push r12 |
| 19 | + push r13 |
| 20 | + |
| 21 | + mov r12, rdi # Loop variable |
| 22 | + lea r13, [rdi + 4*rsi] # Pointer after the last element |
| 23 | + |
| 24 | +print_array_loop: |
| 25 | + cmp r12, r13 # If we're done iterating over the array then bail |
| 26 | + jge print_array_done |
| 27 | + mov rdi, OFFSET array_fmt # Otherwise print the current value |
| 28 | + mov esi, DWORD PTR [r12] |
| 29 | + xor rax, rax |
| 30 | + call printf |
| 31 | + lea r12, [r12 + 4] # And increment the loop variable pointer |
| 32 | + jmp print_array_loop |
| 33 | + |
| 34 | +print_array_done: |
| 35 | + mov rdi, OFFSET lf # Print a closing newline |
| 36 | + xor rax, rax |
| 37 | + call printf |
| 38 | + |
| 39 | + pop r13 |
| 40 | + pop r12 |
| 41 | + ret |
| 42 | + |
| 43 | +bubble_sort: |
| 44 | + xor rcx, rcx # The outer loop counter |
| 45 | + lea rdx, [rdi + 4*rsi - 4] # The end address for the inner loop |
| 46 | + |
| 47 | +outer_loop: |
| 48 | + cmp rcx, rsi # We first check if the outer loop is done |
| 49 | + jge bubble_sort_done # And if it is, return |
| 50 | + mov rax, rdi # Otherwise we initialize the loop variable of the inner loop |
| 51 | +inner_loop: |
| 52 | + mov r8d, DWORD PTR [rax] # Load array[j] and array[j+1] through a pointer |
| 53 | + mov r9d, DWORD PTR [rax + 4] |
| 54 | + cmp r8d, r9d # If array[j] <= array[j+1] |
| 55 | + jle loop_counters # Then we can skip this iteration |
| 56 | + mov DWORD PTR [rax], r9d # Otherwise swap the values |
| 57 | + mov DWORD PTR [rax + 4], r8d |
| 58 | +loop_counters: |
| 59 | + lea rax, [rax + 4] # First, advance the inner loop |
| 60 | + cmp rax, rdx |
| 61 | + jl inner_loop # And in case it's not done, repeat |
| 62 | + inc rcx # If it is done, go back to doing the outer loop |
| 63 | + jmp outer_loop |
| 64 | + |
| 65 | +bubble_sort_done: |
| 66 | + ret |
| 67 | + |
| 68 | +main: |
| 69 | + # Set up our stack |
| 70 | + sub rsp, 40 |
| 71 | + |
| 72 | + # We load the array in chunks onto the stack |
| 73 | + movaps xmm0, XMMWORD PTR [array] |
| 74 | + movaps XMMWORD PTR [rsp], xmm0 |
| 75 | + movaps xmm0, XMMWORD PTR [array + 16] |
| 76 | + movaps XMMWORD PTR [rsp + 16], xmm0 |
| 77 | + mov rax, QWORD PTR [array + 32] |
| 78 | + mov QWORD PTR [rsp + 32], rax |
| 79 | + |
| 80 | + # Print the unsorted array |
| 81 | + mov rdi, OFFSET unsorted |
| 82 | + xor rax, rax |
| 83 | + call printf |
| 84 | + |
| 85 | + mov rdi, rsp |
| 86 | + mov rsi, array_len |
| 87 | + call print_array |
| 88 | + |
| 89 | + # Sort |
| 90 | + mov rdi, rsp |
| 91 | + mov rsi, array_len |
| 92 | + call bubble_sort |
| 93 | + |
| 94 | + # Print the sorted array |
| 95 | + mov rdi, OFFSET sorted |
| 96 | + xor rax, rax |
| 97 | + call printf |
| 98 | + |
| 99 | + mov rdi, rsp |
| 100 | + mov rsi, array_len |
| 101 | + call print_array |
| 102 | + |
| 103 | + # Restore the stack pointer, set return value to 0 |
| 104 | + add rsp, 40 |
| 105 | + xor rax, rax |
| 106 | + ret |
0 commit comments