aboutsummaryrefslogtreecommitdiff
path: root/src/file_parser.s
blob: b63708fc623277a7e9b79bdfa99b3098724f211c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# --------------------------------------------
# FUNCTION: parse_file
# PURPOSE : Parses the file into the array format.
# INPUTS  : rdi = File descriptor
# OUTPUTS : rax = address of array
#           rdx = length of array in 8 byte chunks
# 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, %rax     # Return pointer to array
    movq %r12, %rdx     # Return Number of coordinates

    # Restore registers
    pop %r15
    pop %r14
    pop %r13
    pop %r12
    pop %rbp
    pop %rbx

    ret