From 57b0724d2de125a5116f9ac035b0f19c4fc93243 Mon Sep 17 00:00:00 2001 From: Andreas Kapp Lindquist Date: Tue, 30 Sep 2025 15:48:00 +0200 Subject: feat(file_parser.s): Function for parsing file into array format --- src/file_parser.s | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/file_parser.s (limited to 'src/file_parser.s') 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 -- cgit v1.2.3-70-g09d2