aboutsummaryrefslogtreecommitdiff
path: root/src/quicksort.s
diff options
context:
space:
mode:
authormithe24 <mithe24@student.sdu.dk>2025-10-30 10:28:24 +0100
committermithe24 <mithe24@student.sdu.dk>2025-10-30 10:28:27 +0100
commit597022b22e0e499a567d29c7248e90b7726e0770 (patch)
tree063c35ca2b99f553ecb0ef3cc7684b6eef719e9a /src/quicksort.s
parentd99c832dac240735f5aa282469762bab04c6a45c (diff)
downloadsorter-597022b22e0e499a567d29c7248e90b7726e0770.tar.gz
sorter-597022b22e0e499a567d29c7248e90b7726e0770.zip
consistent names
Diffstat (limited to 'src/quicksort.s')
-rwxr-xr-xsrc/quicksort.s136
1 files changed, 0 insertions, 136 deletions
diff --git a/src/quicksort.s b/src/quicksort.s
deleted file mode 100755
index 54720bf..0000000
--- a/src/quicksort.s
+++ /dev/null
@@ -1,136 +0,0 @@
-# --------------------------------------------
-# FUNCTION: quicksort
-# PURPOSE : Calls the _quicksort function with the right parameters
-# INPUTS : rdi = address for A
-# rsi = number of coordinates n
-# rdx = index of key to sort by
-# OUTPUTS : rax = address for sorted A
-# --------------------------------------------
-.section .text
-.globl quicksort
-.type quicksort, @function
-quicksort:
- leaq -1(%rsi), %rcx
- xorq %rsi, %rsi
- call _quicksort
- ret
-
-# --------------------------------------------
-# FUNCTION: _quicksort
-# PURPOSE : Sorts array A, with references to arrays A_i,
-# based on the given key in A_i,
-# address and size of A.
-# INPUTS : rdi = address for A
-# rsi = low
-# rdx = index of key to sort by
-# rcx = high
-# OUTPUTS : rax = address for sorted A
-# NOTES : preserves rdi, rdx
-# --------------------------------------------
-.section .text
-# .globl _quicksort
-.type _quicksort, @function
-_quicksort:
- pushq %r12
- pushq %r13
- pushq %r14
-
- cmpq $0, %rsi # if 0 < low
- jl .qsort_exit
- cmpq $0, %rcx # if 0 < high
- jl .qsort_exit
- cmpq %rcx, %rsi # if high < low
- jge .qsort_exit
-
- call hoare_part # rax = p
-
- movq %rsi, %r12 # save low
- movq %rcx, %r13 # save high
- movq %rax, %r14 # save p
-
-# --- left recursion ---
- movq %r12, %rsi # low
- movq %r14, %rcx # high = p
- call _quicksort
-
-# --- right recursion ---
- leaq 1(%r14), %rsi # low = p + 1
- movq %r13, %rcx # high
- call _quicksort
-
-.qsort_exit:
- movq %rdi, %rax
-
- popq %r14
- popq %r13
- popq %r12
- ret
-
-# --------------------------------------------
-# FUNCTION: hoare_part
-# PURPOSE : Sorts array A, with references to arrays A_i,
-# based on the given key in A_i,
-# address and size of A.
-# INPUTS : rdi = address for A
-# rsi = low
-# rdx = index of key to sort by
-# rcx = high
-# OUTPUTS : rax = address for sorted A
-# NOTES : preserves rdi, rdx
-# --------------------------------------------
-.section .text
-.globl hoare_part
-.type hoare_part, @function
-hoare_part:
- pushq %rbx
- pushq %r12
- pushq %r13
- pushq %r14
-
- leaq -1(%rsi), %rax # i = low - 1
- leaq 1(%rcx), %rbx # j = high + 1
-
- # r12 = pivot
- movq (%rdi, %rsi, 8), %r12 # &A[low]
- movq (%r12, %rdx, 8), %r12 # A[low][idx]
-
-# left index
-.hoare_part_left:
- incq %rax
-
- movq (%rdi, %rax, 8), %r13 # &A[i]
- movq (%r13, %rdx, 8), %r13 # A[i][idx]
-
- cmpq %r12, %r13 # while pivot < A[i][idx]
- jl .hoare_part_left
-
-# right index
-.hoare_part_right:
- decq %rbx
-
- movq (%rdi, %rbx, 8), %r13 # &A[j]
- movq (%r13, %rdx, 8), %r13 # A[j][idx]
-
- cmpq %r12, %r13 # while pivot > A[j][idx]
- jg .hoare_part_right
-
-# check crossing
- cmpq %rbx, %rax # i >= j
- jge .hoare_part_done
-
-# swap A[i] and A[j]
- movq (%rdi, %rax, 8), %r13
- movq (%rdi, %rbx, 8), %r14
- movq %r14, (%rdi, %rax, 8)
- movq %r13, (%rdi, %rbx, 8)
-
- jmp .hoare_part_left
-
-.hoare_part_done:
- movq %rbx, %rax # return j as partion index
-
- popq %r14
- popq %r13
- popq %r12
- popq %rbx
- ret