final clean up
[sped.git] / fileutils.asm
1 ; sped - the stupidly pointless editor
2 ; written by pinosaur
3 ; fileutils.asm: file i/o
4
5 %include "macros.S"
6
7 extern printf
8 extern malloc
9 extern realloc
10 extern free
11 extern memset
12 extern strlen
13
14 global readFile
15 global readLine
16 global writeFile
17
18 section .data
19     wrongfile_str db `unable to open file, error code: %i\n`, 0x00
20     returnvalue_str db `system call return was %i\n`, 0x00
21
22 section .text
23
24 ; reads file line by line
25 ; args: filename
26 ; return:
27 ;    eax - pointer to mem
28 ;    ecx - lines read
29 readFile:
30     %define _FILE_NAME  8
31     %define FILE_HANDLE 4
32     %define IS_EOF      8
33     %define LINES_READ  12
34     %define BUF_PTR     16 ; malloced array of strings
35
36     push ebp
37     mov ebp, esp
38     
39     ; allocate vars
40     sub esp, 16
41     mov DWORD [ebp-FILE_HANDLE], 0x00
42     mov DWORD [ebp-IS_EOF], 0x00
43     mov DWORD [ebp-LINES_READ], 0x00
44
45     push 0
46     call malloc 
47     mov [ebp-BUF_PTR], eax
48
49     ; open existing file
50     mov eax, 5
51     mov ebx, [ebp+_FILE_NAME]
52     mov ecx, 0
53     mov edx, 0777
54     int 0x80
55     mov [ebp-FILE_HANDLE], eax
56
57     ; check if file was open successfully
58     cmp eax, 0
59     jge _readFile_loop
60     push eax
61     push wrongfile_str
62     call printf
63     jmp _readFile_exit
64
65     _readFile_loop:
66
67     push DWORD [ebp-FILE_HANDLE]
68     call readLine 
69     mov esi, eax
70     mov [ebp-IS_EOF], ebx
71
72     ; check if eof was reached
73     cmp DWORD [ebp-IS_EOF], 1
74     je _readFile_exit
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     ; check for eof
164     cmp eax, 0 ; eax has zero on eof
165     jne _readLine_not_eof
166     mov ebx, 1
167     jmp _readLine_exit 
168     _readLine_not_eof:
169
170     ; check for newline
171     mov eax, [ebp-STR_PTR]
172     add eax, [ebp-CHAR_COUNT]
173     cmp DWORD [eax], 0x0a
174     jne _readLine_not_newline
175     mov ebx, 0
176     jmp _readLine_exit
177     _readLine_not_newline:
178
179     add DWORD [ebp-CHAR_COUNT], 1
180     jmp _readLine_loop
181
182     _readLine_exit:
183
184     mov eax, [ebp-BLOCK_COUNT]
185     mov ecx, 63
186     mul ecx
187     add eax, [ebp-CHAR_COUNT]
188     mov ecx,eax
189
190     mov eax, DWORD [ebp-STR_PTR]
191
192     %undef _FILE_HANDLE
193     %undef CHAR_COUNT
194     %undef BLOCK_COUNT 
195     %undef STR_PTR
196
197     mov esp, ebp
198     pop ebp
199     ret
200
201 ; writes contents of string array into file
202 ; arg: filename, string array, number of lines
203 writeFile:
204
205     %define _FILE_NAME    16
206     %define _STR_ARR      12
207     %define _STR_ARR_LEN  8
208     %define FILE_HANDLE   4
209     %define LINES_WRITTEN 8
210
211     push ebp
212     mov ebp, esp
213
214     ; allocate vars
215     sub esp, 8
216     mov DWORD [ebp-FILE_HANDLE], 0x00
217     mov DWORD [ebp-LINES_WRITTEN], 0x00
218
219     ; open existing file
220     mov eax, 5
221     mov ebx, [ebp+_FILE_NAME]
222     mov ecx, 2
223     mov edx, 0777
224     int 0x80
225     mov [ebp-FILE_HANDLE], eax
226
227     ; check if file was open successfully
228     cmp eax, 0
229     jl _writeFile_error
230
231     ; truncate file
232     mov eax, 93
233     mov ebx, [ebp-FILE_HANDLE]
234     mov ecx, 1
235     int 0x80
236     
237     _writeFile_loop:
238
239     ; check if we are done writing
240     mov eax, [ebp+_STR_ARR_LEN]
241     cmp eax, [ebp-LINES_WRITTEN]
242     je _writeFile_exit
243
244     ; get length of string to write
245     str_offset [ebp+_STR_ARR], [ebp-LINES_WRITTEN]
246     mov esi, eax
247     push DWORD [esi]
248     call strlen
249
250     mov edx, eax
251     mov eax, 4
252     mov ebx, [ebp-FILE_HANDLE]    
253     mov ecx, [esi]
254     int 0x80
255
256     add DWORD [ebp-LINES_WRITTEN], 1
257
258     jmp _writeFile_loop
259
260     _writeFile_error:
261     push eax
262     push wrongfile_str
263     call printf
264     jmp _writeFile_exit
265
266     _writeFile_exit:
267
268     ; close file
269     mov eax, 6
270     mov ebx, [ebp-FILE_HANDLE]
271     int 0x80
272     
273     %undef _FILE_NAME
274     %undef _STR_ARR
275     %undef _STR_ARR_LEN
276     %undef FILE_HANDLE
277     
278     mov esp, ebp
279     pop ebp
280     ret
281