starting on repl
[sped.git] / fileutils.asm
1
2 extern printf
3 extern malloc
4 extern realloc
5 extern free
6 extern memset
7
8 global readFile
9 global readLine
10
11 section .data
12     wrongfile_str db `unable to open file, error code: %i\n`, 0x00
13
14 section .text
15
16 ; reads file line by line
17 ; args: filename
18 ; return:
19 ;    eax - pointer to mem
20 ;    ecx - lines read
21 readFile:
22     %define _FILE_NAME  8
23     %define FILE_HANDLE 4
24     %define IS_EOF      8
25     %define LINES_READ  12
26     %define BUF_PTR     16 ; malloced array of strings
27
28     push ebp
29     mov ebp, esp
30     
31     ; allocate vars
32     sub esp, 16
33     mov DWORD [ebp-FILE_HANDLE], 0x00
34     mov DWORD [ebp-IS_EOF], 0x00
35     mov DWORD [ebp-LINES_READ], 0x00
36
37     push 0
38     call malloc 
39     mov [ebp-BUF_PTR], eax
40
41     ; open existing file
42     mov eax, 5
43     mov ebx, [ebp+_FILE_NAME]
44     mov ecx, 0
45     mov edx, 0777
46     int 0x80
47     mov [ebp-FILE_HANDLE], eax
48
49     ; check if file was open successfully
50     cmp eax, 0
51     jge _readFile_loop
52     push eax
53     push wrongfile_str
54     call printf
55     jmp _readFile_exit
56
57     _readFile_loop:
58
59     ; check if eof was reached
60     cmp DWORD [ebp-IS_EOF], 1
61     je _readFile_exit
62
63     push DWORD [ebp-FILE_HANDLE]
64     call readLine 
65
66     mov esi, eax
67     mov [ebp-IS_EOF], ebx
68     
69     ; push esi
70     ; call printf
71
72     ; make string buffer bigger
73     mov eax, DWORD [ebp-LINES_READ]
74     add eax, 1
75     mov ecx, 4
76     mul ecx
77     push eax
78     push DWORD [ebp-BUF_PTR]
79     call realloc
80     mov DWORD [ebp-BUF_PTR], eax
81
82     ; write string to buffer
83     mov eax, DWORD [ebp-BUF_PTR]
84     mov ecx, 4
85     mul ecx
86     mov eax, esi
87
88     add DWORD [ebp-LINES_READ], 1
89
90     jmp _readFile_loop
91
92     _readFile_exit:
93     ; close file
94     mov eax, 6
95     mov ebx, [ebp-FILE_HANDLE]
96     int 0x80
97
98     mov eax, [ebp-BUF_PTR]
99     mov ecx, [ebp-LINES_READ]
100
101     %undef _FILE_NAME
102     %undef FILE_HANDLE
103     %undef IS_EOF
104     %undef LINES_READ
105     %undef BUF_PTR
106
107     mov esp, ebp
108     pop ebp
109     ret
110
111
112 ; reads a line until newline character is reached
113 ; args: file_handle
114 ; return:
115 ;   eax: location to buffer
116 ;   ebx: contains eof
117 ;   ecx: number of chars read
118 readLine:
119     %define _FILE_HANDLE 8
120     %define CHAR_COUNT   4   ; count number of characters read
121     %define BLOCK_COUNT  8   ; number of 64 blocks we've read
122     %define STR_PTR      12  ; malloced buffer to store read string
123
124     push ebp
125     mov ebp, esp
126     
127     ; allocate vars
128     sub esp, 12
129     mov DWORD [ebp-CHAR_COUNT], 0x00
130     mov DWORD [ebp-BLOCK_COUNT], 0x00
131
132     push 64
133     call malloc
134     mov [ebp-STR_PTR], eax
135
136     push DWORD [ebp-STR_PTR]
137     push 0x00
138     push 64
139
140     _readLine_loop:
141     ; if buffer is full
142     cmp BYTE [ebp-CHAR_COUNT], 63 ; leave one byte for null byte
143     jne _readLine_notfull
144     jmp _readLine_exit
145
146     _readLine_notfull:
147     ; read a single character
148     mov eax, 3
149     mov ebx, [ebp+_FILE_HANDLE]
150     mov ecx, [ebp-STR_PTR]
151     add ecx, [ebp-CHAR_COUNT]
152     mov edx, 1
153     int 0x80
154
155     ; mov eax, 4
156     ; mov ebx, 1
157     ; mov ecx, [ebp-STR_PTR]
158     ; add ecx, [ebp-CHAR_COUNT]
159     ; mov edx, 1
160     ; int 0x80
161     
162     ; check for eof
163     cmp eax, 0 ; eax has zero on eof
164     jne _readLine_not_eof
165     mov ebx, 1
166     jmp _readLine_exit 
167     _readLine_not_eof:
168
169     ; check for newline
170     mov eax, [ebp-STR_PTR]
171     add eax, [ebp-CHAR_COUNT]
172     cmp DWORD [eax], 0x0a
173     jne _readLine_not_newline
174     mov ebx, 0
175     jmp _readLine_exit
176     _readLine_not_newline:
177
178     add DWORD [ebp-CHAR_COUNT], 1
179     jmp _readLine_loop
180
181     _readLine_exit:
182
183     mov eax, [ebp-BLOCK_COUNT]
184     mov ecx, 63
185     mul ecx
186     add eax, [ebp-CHAR_COUNT]
187     mov ecx,eax
188
189     mov eax, DWORD [ebp-STR_PTR]
190
191     %undef _FILE_HANDLE
192     %undef CHAR_COUNT
193     %undef BLOCK_COUNT 
194     %undef STR_PTR
195
196     mov esp, ebp
197     pop ebp
198     ret
199