1 ; sped - the stupidly pointless editor
3 ; fileutils.asm: file i/o
19 wrongfile_str db `unable to open file, error code: %i\n`, 0x00
20 returnvalue_str db `system call return was %i\n`, 0x00
24 ; reads file line by line
27 ; eax - pointer to mem
34 %define BUF_PTR 16 ; malloced array of strings
41 mov DWORD [ebp-FILE_HANDLE], 0x00
42 mov DWORD [ebp-IS_EOF], 0x00
43 mov DWORD [ebp-LINES_READ], 0x00
47 mov [ebp-BUF_PTR], eax
51 mov ebx, [ebp+_FILE_NAME]
55 mov [ebp-FILE_HANDLE], eax
57 ; check if file was open successfully
67 push DWORD [ebp-FILE_HANDLE]
72 ; check if eof was reached
73 cmp DWORD [ebp-IS_EOF], 1
76 ; make string buffer bigger
77 mov eax, DWORD [ebp-LINES_READ]
82 push DWORD [ebp-BUF_PTR]
84 mov DWORD [ebp-BUF_PTR], eax
86 ; write string to buffer
87 mov eax, [ebp-LINES_READ]
90 add eax, DWORD [ebp-BUF_PTR]
96 add DWORD [ebp-LINES_READ], 1
103 mov ebx, [ebp-FILE_HANDLE]
106 mov eax, [ebp-BUF_PTR]
107 mov ecx, [ebp-LINES_READ]
120 ; reads a line until newline character is reached
123 ; eax: location to buffer
125 ; ecx: number of chars read
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
137 mov DWORD [ebp-CHAR_COUNT], 0x00
138 mov DWORD [ebp-BLOCK_COUNT], 0x00
142 mov [ebp-STR_PTR], eax
144 push DWORD [ebp-STR_PTR]
150 cmp BYTE [ebp-CHAR_COUNT], 63 ; leave one byte for null byte
151 jne _readLine_notfull
155 ; read a single character
157 mov ebx, [ebp+_FILE_HANDLE]
158 mov ecx, [ebp-STR_PTR]
159 add ecx, [ebp-CHAR_COUNT]
164 cmp eax, 0 ; eax has zero on eof
165 jne _readLine_not_eof
171 mov eax, [ebp-STR_PTR]
172 add eax, [ebp-CHAR_COUNT]
173 cmp DWORD [eax], 0x0a
174 jne _readLine_not_newline
177 _readLine_not_newline:
179 add DWORD [ebp-CHAR_COUNT], 1
184 mov eax, [ebp-BLOCK_COUNT]
187 add eax, [ebp-CHAR_COUNT]
190 mov eax, DWORD [ebp-STR_PTR]
201 ; writes contents of string array into file
202 ; arg: filename, string array, number of lines
205 %define _FILE_NAME 16
207 %define _STR_ARR_LEN 8
208 %define FILE_HANDLE 4
209 %define LINES_WRITTEN 8
216 mov DWORD [ebp-FILE_HANDLE], 0x00
217 mov DWORD [ebp-LINES_WRITTEN], 0x00
221 mov ebx, [ebp+_FILE_NAME]
225 mov [ebp-FILE_HANDLE], eax
227 ; check if file was open successfully
233 mov ebx, [ebp-FILE_HANDLE]
239 ; check if we are done writing
240 mov eax, [ebp+_STR_ARR_LEN]
241 cmp eax, [ebp-LINES_WRITTEN]
244 ; get length of string to write
245 str_offset [ebp+_STR_ARR], [ebp-LINES_WRITTEN]
252 mov ebx, [ebp-FILE_HANDLE]
256 add DWORD [ebp-LINES_WRITTEN], 1
270 mov ebx, [ebp-FILE_HANDLE]