diff options
| author | Andreas Kapp Lindquist <alind24@student.sdu.dk> | 2025-10-09 11:59:28 +0200 |
|---|---|---|
| committer | mithe24 <mithe24@student.sdu.dk> | 2025-10-29 13:49:57 +0100 |
| commit | f7c6c5b1e769132b381d4625ac3a701bb7c41198 (patch) | |
| tree | f0b8f6796205475de9046f6082a333ce63084e8d /src/array_maker.s | |
| parent | d4019785614605ffa363025de8527e4c1e67180e (diff) | |
| download | sorter-f7c6c5b1e769132b381d4625ac3a701bb7c41198.tar.gz sorter-f7c6c5b1e769132b381d4625ac3a701bb7c41198.zip | |
feat(array_maker): new file for creating the array structure
Diffstat (limited to 'src/array_maker.s')
| -rw-r--r-- | src/array_maker.s | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/src/array_maker.s b/src/array_maker.s new file mode 100644 index 0000000..d4c0afc --- /dev/null +++ b/src/array_maker.s @@ -0,0 +1,154 @@ +# -------------------------------------------- +# FUNCTION: make_array_from_file +# PURPOSE : Take a file descriptor, and convert it to the array data structure +# INPUTS : rdi = File descriptor +# OUTPUTS : rax = Pointer to start of array in memory +# rdx = Length of array in 8 byte chunks (the number of elements) +# CLOBBERS: rax, rsi +# -------------------------------------------- +.globl make_array_from_file +.type make_array_from_file, @function + make_array_from_file: + + push %r15 + push %r14 + push %r13 + push %r12 + + # The file descriptor is in rdi + call create_file_buffer + movq %rax, %rdi # Select pointer to file data + movq %rdx, %rsi # Select length of file data + + call make_pairwise_data # Make the pairwise data + movq %rax, %r15 # Save pointer to pariwise data in r15 + movq %rdx, %r14 # Save the number of pairs in r14 + + # Allocate space for the array + movq %r14, %rdi # Select the number of coordinates + imulq $8, %rdi # 8 byte elements in array. Space for pointers + call allocate + movq %rax, %r13 # Save array pointer in r13 + + # Add pointers in array that point to pairwise array. Pointer 0 + # points to pair 0 and so on. + movq $0, %r12 # Counter for displacing pointers in array +loop: + cmp %r14, %r12 # Compare number of pairs with the counter + je end + + movq %r15, (%r13, %r12, 8) # Save pointer to pair_i in position_i in array + + addq $1, %r12 # Go to next spot in array + addq $16, %r15 # Go to next pair + + jmp loop +end: + movq %r13, %rax # Return pointer to array + movq %r14, %rdx # Return the number of elements in the array + + pop %r12 + pop %r13 + pop %r14 + pop %r15 + + ret + +# -------------------------------------------- +# FUNCTION: make_pairwise_data +# PURPOSE : Take the raw data from the file, and convert it to an array of x,y +# coordinates side by side: [x_1,y_1,x_2,y_2,...] +# INPUTS : rdi = Pointer to start of file data in memory +# rsi = Size of file +# OUTPUTS : rax = Pointer to start of pairwise data in memory +# rdx = The number of pairs +# CLOBBERS: rdi, rsi, rdx, rax +# -------------------------------------------- +.globl make_pairwise_data +.type make_pairwise_data, @function + make_pairwise_data: + push %r15 + push %r14 + push %r13 + push %r12 + + movq %rdi, %r15 # Save file pointer in r15 + movq %rsi, %r14 # Save file size in r14 + + # Count number of lines + movq %r15, %rdi # Select the file pointer + movq %r14, %rsi # Select the file size + call getLineCount + 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 + imulq $16, %r13 # Multiply number of coordinates with 2*8 + movq %r13, %rdi # Select the number of coordinates + call allocate + movq %rax, %r12 # Save pointer to where pairwise data will be placed + + # Parse the data from the file into the pairwise data, and place it into the + # allocated space + movq %r15, %rdi # Select the pointer to the file data + movq %r14, %rsi # Select size of file + movq %r12, %rdx # Select memory location for where to put data + call parseData + + # Divide r13 with 16, this is to go from total length of array to the number + # of coordinates + movq %r13, %rax # Use r13 as dividend + movq $16, %rdi # Use 16 as divisor + cqo # Sign extend rax into rdx + idivq %rdi # r13/16 -> quotient in rax + + movq %rax, %rdx # Return number of pairs (the quotient of division) + movq %r12, %rax # Return pointer to pairwise data + + pop %r12 + pop %r13 + pop %r14 + pop %r15 + + ret + +# -------------------------------------------- +# FUNCTION: create_file_buffer +# PURPOSE : Take a file descriptor and create a memory location and put the +# data of the file in that location. +# INPUTS : rdi = File descriptor +# OUTPUTS : rax = Pointer to start of file in memory +# rdx = Length of file in bytes +# CLOBBERS: rax, rsi, rdi, rdx +# -------------------------------------------- +.globl create_file_buffer +.type create_file_buffer, @function + create_file_buffer: + + push %r15 + push %r14 + push %r13 + + movq %rdi, %r15 # Save file descriptor in r15 + movq %r15, %rdi # Select file descriptor + call getFileSize # Get the file size + movq %rax, %r14 # Save file size in r14 + + movq %r14, %rdi # Select file size + call allocate # Allocate memory for file contents + movq %rax, %r13 # Save file buffer in r13 + + movq %r15, %rdi # Select the file discriptor + movq %r13, %rsi # Select the buffer to store input + movq %r14, %rdx # Size to read from file + movq $0, %rax # Select read syscall + syscall # Read from the file + + movq %r13, %rax # Return pointer to start of file + movq %r14, %rdx # Return length of file + + pop %r13 + pop %r14 + pop %r15 + + ret |