From e0d2d485a3f344edf267df643c9b08b6fa77c899 Mon Sep 17 00:00:00 2001 From: Navid Samanghoon Date: Tue, 21 Oct 2025 13:34:36 +0200 Subject: update: Improved code readability and clarity by refining comments --- src/insertion_sort.s | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'src/insertion_sort.s') diff --git a/src/insertion_sort.s b/src/insertion_sort.s index ed4ade2..f5c38ad 100644 --- a/src/insertion_sort.s +++ b/src/insertion_sort.s @@ -12,41 +12,45 @@ .globl insertion_sort .type insertion_sort, @function insertion_sort: + # save push %r8 push %r9 push %r10 push %r11 push %r14 push %r15 - cmp $1, %rsi # n <= 1 -> done + + + cmp $1, %rsi # if n <= 1 jmp done jle done - movq $1, %r15 # i , start with 2nd element + # set up i + movq $1, %r15 # i , start with 2nd element loop: # load A[i] - movq (%rdi,%r15,8), %r8 # pointer to key -> r8 - movq (%r8,%rdx,8), %r9 # key -> r9 + movq (%rdi,%r15,8), %r8 # pointer to key -> r8 + movq (%r8,%rdx,8), %r9 # key -> r9 - # set up index - 1 now - movq %r15, %r14 # j -> r14 + # set up j + movq %r15, %r14 # j -> r14 decq %r14 inner_loop: - cmpq $0, %r14 # if j < 0 stop + cmpq $0, %r14 # if j < 0 stop and insert the key jl insert_key # load A[j] - movq (%rdi, %r14, 8), %r10 # pointer to key -> r10 - movq (%r10, %rdx, 8), %r11 # key -> r11 + movq (%rdi, %r14, 8), %r10 # pointer to key -> r10 + movq (%r10, %rdx, 8), %r11 # key -> r11 - # compare at A[i] with A[j] + # compare A[i] with A[j] cmpq %r11, %r9 jge insert_key # if A[i] >= A[j] stop # if A[i] < A[j] - movq %r10, 8(%rdi, %r14, 8) # pointer to key of j -> A[j+1] + movq %r10, 8(%rdi, %r14, 8) # pointer to key of j -> A[j+1] decq %r14 # move to j - 1 jmp inner_loop @@ -55,14 +59,16 @@ inner_loop: insert_key: movq %r8, 8(%rdi,%r14,8) - # take the next number now - incq %r15 # i++ - cmpq %rsi, %r15 # if i < n there are still numbers left to be sorted + # take the next number + incq %r15 # i++ + cmpq %rsi, %r15 # if i < n there are still numbers left to be sorted jl loop jmp done done: movq %rdi, %rax + + # retrieve pop %r15 pop %r14 pop %r11 -- cgit v1.2.3-70-g09d2