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/array_maker.s | 9 +++++++-- src/insertion_sort.s | 34 ++++++++++++++++++++-------------- src/main.s | 34 ++++++++++++++++++---------------- src/outputGenerator.s | 47 ++++++++++++++++++++++++++++------------------- 4 files changed, 73 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/array_maker.s b/src/array_maker.s index d4c0afc..7586d84 100644 --- a/src/array_maker.s +++ b/src/array_maker.s @@ -10,6 +10,7 @@ .type make_array_from_file, @function make_array_from_file: + # save push %r15 push %r14 push %r13 @@ -47,6 +48,7 @@ end: movq %r13, %rax # Return pointer to array movq %r14, %rdx # Return the number of elements in the array + # retrieve pop %r12 pop %r13 pop %r14 @@ -67,6 +69,7 @@ end: .globl make_pairwise_data .type make_pairwise_data, @function make_pairwise_data: + # save push %r15 push %r14 push %r13 @@ -82,7 +85,7 @@ end: movq %rax, %r13 # Save number of lines (coordinates) in r13 # Allocate space for the pairwise coordinates after parseData - # The space allocated should be 2*(#coordinates)*8 bytes + # The space allocated = 2*(#coordinates)*8 bytes imulq $16, %r13 # Multiply number of coordinates with 2*8 movq %r13, %rdi # Select the number of coordinates call allocate @@ -105,6 +108,7 @@ end: movq %rax, %rdx # Return number of pairs (the quotient of division) movq %r12, %rax # Return pointer to pairwise data + # retrieve pop %r12 pop %r13 pop %r14 @@ -124,7 +128,7 @@ end: .globl create_file_buffer .type create_file_buffer, @function create_file_buffer: - + # save push %r15 push %r14 push %r13 @@ -147,6 +151,7 @@ end: movq %r13, %rax # Return pointer to start of file movq %r14, %rdx # Return length of file + # retrieve pop %r13 pop %r14 pop %r15 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 diff --git a/src/main.s b/src/main.s index 56b682c..c0d6e85 100644 --- a/src/main.s +++ b/src/main.s @@ -2,27 +2,29 @@ .globl _start _start: # Open file - movq 16(%rsp), %rdi # Select first argument as file name - movq $0, %rsi # Select read only - movq $0, %rdx # Unused mode for read only - movq $2, %rax # Select open syscall - syscall # Open file, file descriptor returned in rax + movq 16(%rsp), %rdi # Select first argument as file name + movq $0, %rsi # Select read only + movq $0, %rdx # Unused mode for read only + movq $2, %rax # Select open syscall + syscall # Open file, file descriptor returned in rax # Convert to array - movq %rax, %rdi # Select file descriptor - call make_array_from_file # Convert file to array format - movq %rdx, %r15 # Save length of array in r15 + movq %rax, %rdi # Select file descriptor + call make_array_from_file # Convert file to array format + movq %rdx, %r15 # Save length of array in r15 - movq %rax, %rdi # Select address of array - movq %r15, %rsi # Select length of array - movq $1, %rdx # Sort by key 1 + # Sort + movq %rax, %rdi # Select address of array + movq %r15, %rsi # Select length of array + movq $1, %rdx # Sort by key 1 call insertion_sort # Sort the array # Print array - movq %rax, %rdi # Select the pointer to the array - movq %r15, %rsi # Select length of array - call print_buffer # Print array + movq %rax, %rdi # Select the pointer to the array + movq %r15, %rsi # Select length of array + call print_buffer # Print array - movq $60, %rax # Select exit syscall - movq $0, %rdi # Exit code 0 + # Exit + movq $60, %rax + movq $0, %rdi # Exit code 0 syscall diff --git a/src/outputGenerator.s b/src/outputGenerator.s index 34377ba..1e9224b 100644 --- a/src/outputGenerator.s +++ b/src/outputGenerator.s @@ -1,4 +1,5 @@ .section .data +# This buffer holds the ASCII code for tab and newline newline: .byte 10 # '\n' @@ -16,10 +17,10 @@ tab: .globl print_tab .type print_tab, @function print_tab: - movq $1, %rax # write - movq $1, %rdi # stdout - movq $tab, %rsi # load tab character - movq $1, %rdx # 1 byte + movq $1, %rax # write + movq $1, %rdi # stdout + movq $tab, %rsi # load tab character + movq $1, %rdx # 1 byte syscall ret @@ -34,10 +35,10 @@ print_tab: .globl print_newline .type print_newline, @function print_newline: - movq $1, %rax # write - movq $1, %rdi # stdout - movq $newline, %rsi # load newline character - movq $1, %rdx # 1 byte + movq $1, %rax # write + movq $1, %rdi # stdout + movq $newline, %rsi # load newline character + movq $1, %rdx # 1 byte syscall ret @@ -55,13 +56,13 @@ print_buffer: cmpq $0, %rsi jle done # if n > 0 print_coordinate - movq %rdi, %rdx # keep pointer here, rdi will be used for print_num + movq %rdi, %rdx # save pointer, rdi is used for print_num loop: movq (%rdx), %r15 # Pointer element # print x and tab - movq (%r15), %rdi # Get first number from element + movq (%r15), %rdi # Get 1st number from element call print_num # Print it push %rdx # save pointer push %rsi # save n @@ -70,7 +71,7 @@ loop: pop %rdx # retrieve pointer # print y and newline - movq 8(%r15), %rdi # Get the second number + movq 8(%r15), %rdi # Get the 2nd number call print_num # Print it push %rdx # save pointer push %rsi # save n @@ -78,6 +79,7 @@ loop: pop %rsi # retrieve n pop %rdx # retrieve pointer + # move to next coordinate and decq n addq $8, %rdx decq %rsi @@ -89,9 +91,16 @@ done: ret -# Print RDI as an unsigned integer without any new line. +# -------------------------------------------- +# FUNCTION: print_num +# PURPOSE : print rdi as an unsigned integer +# INPUTS : rdi +# OUTPUTS : +# CLOBBERS: # Note: the function does not follow the ordinary calling convention, # but restores all registers. +# -------------------------------------------- + .type print_num, @function .globl print_num print_num: @@ -107,22 +116,22 @@ print_num: push %r8 push %r9 - movq %rdi, %rax # arg - movq $0, %r9 # digit count + movq %rdi, %rax # arg + movq $0, %r9 # digit count .Lprint_num_convertLoop: movq $0, %rdx movq $10, %rcx idivq %rcx - addq $48, %rdx # '0' is 48 + addq $48, %rdx # '0' is 48 push %rdx addq $1, %r9 cmpq $0, %rax jne .Lprint_num_convertLoop .Lprint_num_printLoop: - movq $1, %rax # sys_write - movq $1, %rdi # stdout - movq %rsp, %rsi # buf - movq $1, %rdx # len + movq $1, %rax # sys_write + movq $1, %rdi # stdout + movq %rsp, %rsi # buf + movq $1, %rdx # len syscall addq $8, %rsp addq $-1, %r9 -- cgit v1.2.3-70-g09d2