aboutsummaryrefslogtreecommitdiff
path: root/src/array_maker.s
blob: d4c0afc8cb626ac6e039311cc26f26b3fcd4ada6 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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