more work on reading file
[sped.git] / sped.asm
1 ; sped - the stupidly pointless editor
2 ; written by pinosaur
3
4 global main
5 extern printf
6
7 ; macros
8 %macro write_str 2
9     mov eax, 4
10     mov ebx, 1
11     mov ecx, %1
12     mov edx, %2
13     int 0x80
14 %endmacro
15
16 section .data
17     banner_str db `SPED - the stupidly pointless editor\n`, 0x00
18     readfile_str db `reading file %s\n`, 0x00
19     nofile_str db `no file provided\n`, 0x00
20     argcount_str db `there are %d args\n`, 0x00
21     wrongfile_str db `unable to open file, error code: %i\n`, 0x00
22     char_str db `read this char: %i\n`, 0x00
23     printfint_str db `int: %i\n`, 0x00
24
25 section .bss
26     read_buf resb 64
27
28 section .text
29 main:
30     %define _ARGC 8
31     %define _ARGV 12
32
33     push ebp
34     mov ebp, esp
35
36     ; read command line args
37     mov ecx, [ebp+_ARGC]
38     cmp ecx, 1
39     jg _main_existing
40     
41     ; display error msg if no file
42     push nofile_str
43     call printf
44     mov eax, 1
45     jmp _main_exit
46
47     _main_existing:
48     mov ebx, DWORD [ebp+_ARGV]
49     add ebx, 4
50     push DWORD [ebx]
51     ; push readfile_str
52     ; call printf
53
54     call readFile
55
56     mov eax, 0
57     jmp _main_exit
58
59     _main_exit:
60     %undef _ARGC
61     %undef _ARGV
62
63     mov esp, ebp
64     pop ebp
65     ret
66
67
68 ; reads file line by line
69 ; args: filename
70 ; return:
71 ;    eax - pointer to mem
72 ;    ecx - lines read
73 readFile:
74     %define _FILE_NAME 8
75     %define FILE_HANDLE 4
76
77     push ebp
78     mov ebp, esp
79     
80     ; allocate vars
81     sub esp, 4
82     mov DWORD [ebp-FILE_HANDLE], 0x00
83
84     ; open existing file
85     mov eax, 5
86     mov ebx, [ebp+_FILE_NAME]
87     mov ecx, 0
88     mov edx, 0777
89     int 0x80
90     mov [ebp-FILE_HANDLE], eax
91
92     ; check if file was open successfully
93     cmp eax, 0
94     jge _readFile_noerror
95     push eax
96     push wrongfile_str
97     call printf
98     jmp _readFile_exit
99
100     _readFile_noerror:
101     push DWORD [ebp-FILE_HANDLE]
102     call readLine 
103
104     jmp _readFile_exit
105
106     _readFile_exit:
107     ; close file
108     mov eax, 6
109     mov ebx, [ebp-FILE_HANDLE]
110     int 0x80
111
112     %undef _FILE_NAME
113     %undef FILE_HANDLE
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 ;   location to buffer
124 ;   contains eof
125 readLine:
126     %define _FILE_HANDLE 8
127     %define CHAR_PTR 4
128
129     push ebp
130     mov ebp, esp
131
132     sub esp, 4
133     mov DWORD [ebp-CHAR_PTR], 0x00
134
135     _readLine_loop:
136     ; if buffer is full
137     cmp BYTE [ebp-CHAR_PTR], 64
138     jne _readLine_notfull
139     jmp _readLine_exit
140
141     _readLine_notfull:
142     ; read a single character
143     mov eax, 3
144     mov ebx, [ebp+_FILE_HANDLE]
145     mov ecx, read_buf
146     add ecx, [ebp-CHAR_PTR]
147     mov edx, 1
148     int 0x80
149
150     mov eax, 4
151     mov ebx, 1
152     mov ecx, read_buf
153     add ecx, [ebp-CHAR_PTR]
154     mov edx, 1
155     int 0x80
156
157     ; check for newline
158     mov eax, read_buf
159     add eax, [ebp-CHAR_PTR]
160     cmp DWORD [eax], 0x0a
161     je _readLine_exit
162     
163     ; check for eof
164     mov eax, read_buf
165     add eax, [ebp-CHAR_PTR]
166     cmp DWORD [eax], 0x05
167     je _readLine_exit
168
169     add DWORD [ebp-CHAR_PTR], 1
170
171     jmp _readLine_loop
172
173     _readLine_exit:
174
175     %undef _FILE_HANDLE
176     %undef CHAR_PTR
177
178     mov esp, ebp
179     pop ebp
180     ret
181