aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndreas Kapp Lindquist <alind24@student.sdu.dk>2025-09-30 15:48:00 +0200
committermithe24 <mithe24@student.sdu.dk>2025-10-29 13:49:57 +0100
commit57b0724d2de125a5116f9ac035b0f19c4fc93243 (patch)
treed8894fb3406fe749c8dd38b1a6ff3e0ba558cbe4 /src
parente36e76bef4b06992f6e0ffe3f76be0877c7e381d (diff)
downloadsorter-57b0724d2de125a5116f9ac035b0f19c4fc93243.tar.gz
sorter-57b0724d2de125a5116f9ac035b0f19c4fc93243.zip
feat(file_parser.s): Function for parsing file into array format
Diffstat (limited to '')
-rw-r--r--src/file_parser.s58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/file_parser.s b/src/file_parser.s
new file mode 100644
index 0000000..059dabf
--- /dev/null
+++ b/src/file_parser.s
@@ -0,0 +1,58 @@
+# --------------------------------------------
+# FUNCTION: parse_file
+# PURPOSE : Parses the file into the array format.
+# INPUTS : rdi = File descriptor
+# OUTPUTS : rax = address of array
+# CLOBBERS: none
+# NOTES : Preserves all registers.
+# --------------------------------------------
+.section .text
+.globl parse_file
+.type parse_file, @function
+parse_file:
+ # Save registers
+ push %rbx
+ push %rbp
+ push %r12
+ push %r13
+ push %r14
+ push %r15
+
+ 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, %rdi # Select buffer for file contents
+ movq %r14, %rsi # Select file size
+ call getLineCount # Count lines
+ movq %rax, %r12 # Save line count in r12
+
+ movq %r12, %rdi # Select number of coordinates
+ call make_coordinate_buffer # Create buffer
+ movq %rax, %r13 # Override file buffer with coordinate buffer
+
+ movq %r13, %rdi # Select coordinate buffer
+ movq %r12, %rsi # Select number of coordinates
+ call build_pointer_buffer # Create final array of pointers
+ movq %rax, %r13 # Override r13 with pointer to array
+
+ # Restore registers
+ pop %r15
+ pop %r14
+ pop %r13
+ pop %r12
+ pop %rbp
+ pop %rbx
+
+ ret