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