16 wrongfile_str db `unable to open file, error code: %i\n`, 0x00
20 ; reads file line by line
23 ; eax - pointer to mem
30 %define BUF_PTR 16 ; malloced array of strings
37 mov DWORD [ebp-FILE_HANDLE], 0x00
38 mov DWORD [ebp-IS_EOF], 0x00
39 mov DWORD [ebp-LINES_READ], 0x00
43 mov [ebp-BUF_PTR], eax
47 mov ebx, [ebp+_FILE_NAME]
51 mov [ebp-FILE_HANDLE], eax
53 ; check if file was open successfully
63 ; check if eof was reached
64 cmp DWORD [ebp-IS_EOF], 1
67 push DWORD [ebp-FILE_HANDLE]
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]
165 ; mov ecx, [ebp-STR_PTR]
166 ; add ecx, [ebp-CHAR_COUNT]
171 cmp eax, 0 ; eax has zero on eof
172 jne _readLine_not_eof
178 mov eax, [ebp-STR_PTR]
179 add eax, [ebp-CHAR_COUNT]
180 cmp DWORD [eax], 0x0a
181 jne _readLine_not_newline
184 _readLine_not_newline:
186 add DWORD [ebp-CHAR_COUNT], 1
191 mov eax, [ebp-BLOCK_COUNT]
194 add eax, [ebp-CHAR_COUNT]
197 mov eax, DWORD [ebp-STR_PTR]
208 ; writes contents of string array into file
209 ; arg: filename, string array, number of lines
212 %define _FILE_NAME 16
214 %define _STR_ARR_LEN 8
215 %define FILE_HANDLE 4
216 %define LINES_WRITTEN 8
223 mov DWORD [ebp-FILE_HANDLE], 0x00
224 mov DWORD [ebp-LINES_WRITTEN], 0x00
228 mov ebx, [ebp+_FILE_NAME]
232 mov [ebp-FILE_HANDLE], eax
234 ; check if file was open successfully
244 ; check if we are done writing
245 mov eax, [ebp+_STR_ARR_LEN]
246 cmp eax, [ebp-LINES_WRITTEN]
249 ; get length of string to write
250 str_offset [ebp+_STR_ARR], [ebp-LINES_WRITTEN]
257 mov ebx, [ebp-FILE_HANDLE]
261 add DWORD [ebp-LINES_WRITTEN], 1
269 mov ebx, [ebp-FILE_HANDLE]