abstracting
[sped.git] / fileutils.asm
1
2 %include "utils.S"
3
4 extern printf
5 extern malloc
6 extern realloc
7 extern free
8 extern memset
9 extern strlen
10
11 global readFile
12 global readLine
13 global writeFile
14
15 section .data
16     wrongfile_str db `unable to open file, error code: %i\n`, 0x00
17
18 section .text
19
20 ; reads file line by line
21 ; args: filename
22 ; return:
23 ;    eax - pointer to mem
24 ;    ecx - lines read
25 readFile:
26     %define _FILE_NAME  8
27     %define FILE_HANDLE 4
28     %define IS_EOF      8
29     %define LINES_READ  12
30     %define BUF_PTR     16 ; malloced array of strings
31
32     push ebp
33     mov ebp, esp
34     
35     ; allocate vars
36     sub esp, 16
37     mov DWORD [ebp-FILE_HANDLE], 0x00
38     mov DWORD [ebp-IS_EOF], 0x00
39     mov DWORD [ebp-LINES_READ], 0x00
40
41     push 0
42     call malloc 
43     mov [ebp-BUF_PTR], eax
44
45     ; open existing file
46     mov eax, 5
47     mov ebx, [ebp+_FILE_NAME]
48     mov ecx, 0
49     mov edx, 0777
50     int 0x80
51     mov [ebp-FILE_HANDLE], eax
52
53     ; check if file was open successfully
54     cmp eax, 0
55     jge _readFile_loop
56     push eax
57     push wrongfile_str
58     call printf
59     jmp _readFile_exit
60
61     _readFile_loop:
62
63     ; check if eof was reached
64     cmp DWORD [ebp-IS_EOF], 1
65     je _readFile_exit
66
67     push DWORD [ebp-FILE_HANDLE]
68     call readLine 
69
70     mov esi, eax
71     mov [ebp-IS_EOF], ebx
72     
73     ; push esi
74     ; call printf
75
76     ; make string buffer bigger
77     mov eax, DWORD [ebp-LINES_READ]
78     add eax, 1
79     mov ecx, 4
80     mul ecx
81     push eax
82     push DWORD [ebp-BUF_PTR]
83     call realloc
84     mov DWORD [ebp-BUF_PTR], eax
85
86     ; write string to buffer
87     mov eax, [ebp-LINES_READ]
88     mov ecx, 4
89     mul ecx
90     add eax, DWORD [ebp-BUF_PTR]
91     mov [eax], esi
92
93     ; push DWORD [eax]
94     ; call printf
95
96     add DWORD [ebp-LINES_READ], 1
97
98     jmp _readFile_loop
99
100     _readFile_exit:
101     ; close file
102     mov eax, 6
103     mov ebx, [ebp-FILE_HANDLE]
104     int 0x80
105
106     mov eax, [ebp-BUF_PTR]
107     mov ecx, [ebp-LINES_READ]
108
109     %undef _FILE_NAME
110     %undef FILE_HANDLE
111     %undef IS_EOF
112     %undef LINES_READ
113     %undef BUF_PTR
114
115     mov esp, ebp
116     pop ebp
117     ret
118
119
120 ; reads a line until newline character is reached
121 ; args: file_handle
122 ; return:
123 ;   eax: location to buffer
124 ;   ebx: contains eof
125 ;   ecx: number of chars read
126 readLine:
127     %define _FILE_HANDLE 8
128     %define CHAR_COUNT   4   ; count number of characters read
129     %define BLOCK_COUNT  8   ; number of 64 blocks we've read
130     %define STR_PTR      12  ; malloced buffer to store read string
131
132     push ebp
133     mov ebp, esp
134     
135     ; allocate vars
136     sub esp, 12
137     mov DWORD [ebp-CHAR_COUNT], 0x00
138     mov DWORD [ebp-BLOCK_COUNT], 0x00
139
140     push 64
141     call malloc
142     mov [ebp-STR_PTR], eax
143
144     push DWORD [ebp-STR_PTR]
145     push 0x00
146     push 64
147
148     _readLine_loop:
149     ; if buffer is full
150     cmp BYTE [ebp-CHAR_COUNT], 63 ; leave one byte for null byte
151     jne _readLine_notfull
152     jmp _readLine_exit
153
154     _readLine_notfull:
155     ; read a single character
156     mov eax, 3
157     mov ebx, [ebp+_FILE_HANDLE]
158     mov ecx, [ebp-STR_PTR]
159     add ecx, [ebp-CHAR_COUNT]
160     mov edx, 1
161     int 0x80
162
163     ; mov eax, 4
164     ; mov ebx, 1
165     ; mov ecx, [ebp-STR_PTR]
166     ; add ecx, [ebp-CHAR_COUNT]
167     ; mov edx, 1
168     ; int 0x80
169     
170     ; check for eof
171     cmp eax, 0 ; eax has zero on eof
172     jne _readLine_not_eof
173     mov ebx, 1
174     jmp _readLine_exit 
175     _readLine_not_eof:
176
177     ; check for newline
178     mov eax, [ebp-STR_PTR]
179     add eax, [ebp-CHAR_COUNT]
180     cmp DWORD [eax], 0x0a
181     jne _readLine_not_newline
182     mov ebx, 0
183     jmp _readLine_exit
184     _readLine_not_newline:
185
186     add DWORD [ebp-CHAR_COUNT], 1
187     jmp _readLine_loop
188
189     _readLine_exit:
190
191     mov eax, [ebp-BLOCK_COUNT]
192     mov ecx, 63
193     mul ecx
194     add eax, [ebp-CHAR_COUNT]
195     mov ecx,eax
196
197     mov eax, DWORD [ebp-STR_PTR]
198
199     %undef _FILE_HANDLE
200     %undef CHAR_COUNT
201     %undef BLOCK_COUNT 
202     %undef STR_PTR
203
204     mov esp, ebp
205     pop ebp
206     ret
207
208 ; writes contents of string array into file
209 ; arg: filename, string array, number of lines
210 writeFile:
211
212     %define _FILE_NAME    16
213     %define _STR_ARR      12
214     %define _STR_ARR_LEN  8
215     %define FILE_HANDLE   4
216     %define LINES_WRITTEN 8
217
218     push ebp
219     mov ebp, esp
220
221     ; allocate vars
222     sub esp, 8
223     mov DWORD [ebp-FILE_HANDLE], 0x00
224     mov DWORD [ebp-LINES_WRITTEN], 0x00
225
226     ; open existing file
227     mov eax, 5
228     mov ebx, [ebp+_FILE_NAME]
229     mov ecx, 2
230     mov edx, 0777
231     int 0x80
232     mov [ebp-FILE_HANDLE], eax
233
234     ; check if file was open successfully
235     cmp eax, 0
236     jge _writeFile_loop
237     push eax
238     push wrongfile_str
239     call printf
240     jmp _writeFile_exit
241     
242     _writeFile_loop:
243
244     ; check if we are done writing
245     mov eax, [ebp+_STR_ARR_LEN]
246     cmp eax, [ebp-LINES_WRITTEN]
247     je _writeFile_exit
248
249     ; get length of string to write
250     str_offset [ebp+_STR_ARR], [ebp-LINES_WRITTEN]
251     mov esi, eax
252     push DWORD [esi]
253     call strlen
254
255     mov edx, eax
256     mov eax, 4
257     mov ebx, [ebp-FILE_HANDLE]    
258     mov ecx, [esi]
259     int 0x80
260
261     add DWORD [ebp-LINES_WRITTEN], 1
262
263     jmp _writeFile_loop
264
265     _writeFile_exit:
266
267     ; close file
268     mov eax, 6
269     mov ebx, [ebp-FILE_HANDLE]
270     int 0x80
271     
272     %undef _FILE_NAME
273     %undef _STR_ARR
274     %undef _STR_ARR_LEN
275     %undef FILE_HANDLE
276     
277     mov esp, ebp
278     pop ebp
279     ret
280