16 wrongfile_str db `unable to open file, error code: %i\n`, 0x00
17 returnvalue_str db `system call return was %i\n`, 0x00
21 ; reads file line by line
24 ; eax - pointer to mem
31 %define BUF_PTR 16 ; malloced array of strings
38 mov DWORD [ebp-FILE_HANDLE], 0x00
39 mov DWORD [ebp-IS_EOF], 0x00
40 mov DWORD [ebp-LINES_READ], 0x00
44 mov [ebp-BUF_PTR], eax
48 mov ebx, [ebp+_FILE_NAME]
52 mov [ebp-FILE_HANDLE], eax
54 ; check if file was open successfully
64 push DWORD [ebp-FILE_HANDLE]
69 ; check if eof was reached
70 cmp DWORD [ebp-IS_EOF], 1
77 ; make string buffer bigger
78 mov eax, DWORD [ebp-LINES_READ]
83 push DWORD [ebp-BUF_PTR]
85 mov DWORD [ebp-BUF_PTR], eax
87 ; write string to buffer
88 mov eax, [ebp-LINES_READ]
91 add eax, DWORD [ebp-BUF_PTR]
97 add DWORD [ebp-LINES_READ], 1
104 mov ebx, [ebp-FILE_HANDLE]
107 mov eax, [ebp-BUF_PTR]
108 mov ecx, [ebp-LINES_READ]
121 ; reads a line until newline character is reached
124 ; eax: location to buffer
126 ; ecx: number of chars read
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
138 mov DWORD [ebp-CHAR_COUNT], 0x00
139 mov DWORD [ebp-BLOCK_COUNT], 0x00
143 mov [ebp-STR_PTR], eax
145 push DWORD [ebp-STR_PTR]
151 cmp BYTE [ebp-CHAR_COUNT], 63 ; leave one byte for null byte
152 jne _readLine_notfull
156 ; read a single character
158 mov ebx, [ebp+_FILE_HANDLE]
159 mov ecx, [ebp-STR_PTR]
160 add ecx, [ebp-CHAR_COUNT]
166 ; mov ecx, [ebp-STR_PTR]
167 ; add ecx, [ebp-CHAR_COUNT]
172 cmp eax, 0 ; eax has zero on eof
173 jne _readLine_not_eof
179 mov eax, [ebp-STR_PTR]
180 add eax, [ebp-CHAR_COUNT]
181 cmp DWORD [eax], 0x0a
182 jne _readLine_not_newline
185 _readLine_not_newline:
187 add DWORD [ebp-CHAR_COUNT], 1
192 mov eax, [ebp-BLOCK_COUNT]
195 add eax, [ebp-CHAR_COUNT]
198 mov eax, DWORD [ebp-STR_PTR]
209 ; writes contents of string array into file
210 ; arg: filename, string array, number of lines
213 %define _FILE_NAME 16
215 %define _STR_ARR_LEN 8
216 %define FILE_HANDLE 4
217 %define LINES_WRITTEN 8
224 mov DWORD [ebp-FILE_HANDLE], 0x00
225 mov DWORD [ebp-LINES_WRITTEN], 0x00
229 mov ebx, [ebp+_FILE_NAME]
233 mov [ebp-FILE_HANDLE], eax
235 ; check if file was open successfully
241 mov ebx, [ebp-FILE_HANDLE]
247 ; check if we are done writing
248 mov eax, [ebp+_STR_ARR_LEN]
249 cmp eax, [ebp-LINES_WRITTEN]
252 ; get length of string to write
253 str_offset [ebp+_STR_ARR], [ebp-LINES_WRITTEN]
260 mov ebx, [ebp-FILE_HANDLE]
264 add DWORD [ebp-LINES_WRITTEN], 1
278 mov ebx, [ebp-FILE_HANDLE]