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