1 ; sped - the stupidly pointless editor
3 ; repl.asm: user interactions
18 prompt_str db `sped > `, 0x00
19 invalidcmd_str db `invalid command.\n`, 0x00
20 invalidaddr_str db `invalid address.\n`, 0x00
21 oneline_str db `cannot delete line, as there is only one line.\n`, 0x00
22 currentline_str db `current line: %i\n`, 0x00
23 echo_str db `%s`, 0x00 ; print strings without format exploit
28 buffer_filename resb 4
34 ; args: buffer, buffer_lines, buffer_filename
38 %define _BUFFER_LINES 12
39 %define _BUFFER_FILENAME 8
40 %define CMDSTR 4 ; the previous line read from user
48 mov eax, [ebp+_BUFFER]
50 mov eax, [ebp+_BUFFER_LINES]
51 mov [buffer_lines], eax
52 mov eax, [ebp+_BUFFER_FILENAME]
53 mov [buffer_filename], eax
54 mov DWORD [cur_line], 0x00
64 ; read line from stdin
68 mov DWORD [ebp-CMDSTR], eax
70 ; commands are single char for now
75 mov eax, DWORD [ebp-CMDSTR]
78 ; q exists program =-=-=-=-=-=-=-=-=-=-=-=-=
79 mov eax, DWORD [ebp-CMDSTR]
81 jne _repl_cmd_quit_end
85 ; p prints current line =-=-=-=-=-=-=-=-=-=-=
86 mov eax, DWORD [ebp-CMDSTR]
88 jne _repl_cmd_print_end
90 mov eax, DWORD [cur_line]
100 ; n prints the current line number =-=-=-=-=-=-=-=
101 mov eax, DWORD [ebp-CMDSTR]
103 jne _repl_cmd_number_end
105 push DWORD [cur_line]
110 _repl_cmd_number_end:
112 ; - goes to prev line =-=-=-=-=-=-=-=-=-=-=-=-=
113 mov eax, DWORD [ebp-CMDSTR]
115 jne _repl_cmd_decline_end
117 ; make sure we are within bounds
118 mov eax, DWORD [cur_line]
121 jl _repl_invalid_addr
123 sub DWORD [cur_line], 1
126 _repl_cmd_decline_end:
128 ; + goes to next line =-=-=-=-=-=-=-=-=-=-=-=-=
129 mov eax, DWORD [ebp-CMDSTR]
131 jne _repl_cmd_incline_end
133 ; make sure we are within bounds
134 mov eax, DWORD [cur_line]
136 cmp eax, [buffer_lines]
137 jge _repl_invalid_addr
139 add DWORD [cur_line], 1
142 _repl_cmd_incline_end:
144 ; g goes to first line =-=-=-=-=-=-=-=-=-=-=-=-=
145 mov eax, DWORD [ebp-CMDSTR]
147 jne _repl_cmd_jumptop_end
149 mov DWORD [cur_line], 0x00
152 _repl_cmd_jumptop_end:
154 ; G goes to last line =-=-=-=-=-=-=-=-=-=-=-=-=
155 mov eax, DWORD [ebp-CMDSTR]
157 jne _repl_cmd_jumpbot_end
159 mov eax, DWORD [buffer_lines]
161 mov DWORD [cur_line], eax
164 _repl_cmd_jumpbot_end:
166 ; c changes the current line =-=-=-=-=-=-=-=-=-=
167 mov eax, DWORD [ebp-CMDSTR]
169 jne _repl_cmd_change_end
171 ; read a new line to use
189 add eax, DWORD [buffer]
193 _repl_cmd_change_end:
195 ; d delete line =-=-=-=-=-=-=-=-=-=-=-=-=
196 mov eax, DWORD [ebp-CMDSTR]
198 jne _repl_cmd_delete_end
200 ; check to make sure we don't have only one line
201 cmp DWORD [buffer_lines], 1
206 push DWORD [buffer_lines]
207 push DWORD [cur_line]
211 sub DWORD [buffer_lines], 1
213 ; if it's the last line, move one down
214 mov eax, DWORD [buffer_lines]
215 cmp DWORD [cur_line], eax
217 sub DWORD [cur_line], 1
220 _repl_cmd_delete_end:
222 ; o appends text after line =-=-=-=-=-=-=-=-=
223 mov eax, DWORD [ebp-CMDSTR]
225 jne _repl_cmd_appenddown_end
229 push DWORD [buffer_lines]
230 mov eax, DWORD [cur_line]
246 add eax, DWORD [buffer]
249 add DWORD [buffer_lines], 1
252 _repl_cmd_appenddown_end:
254 ; O oppens text before line =-=-=-=-=-=-=-=
255 mov eax, DWORD [ebp-CMDSTR]
257 jne _repl_cmd_appendup_end
261 push DWORD [buffer_lines]
262 push DWORD [cur_line]
275 add eax, DWORD [buffer]
278 ; also move cursor down one
279 add DWORD [cur_line], 1
281 add DWORD [buffer_lines], 1
284 _repl_cmd_appendup_end:
287 ; w writes file =-=-=-=-=-=-=-=-=-=-=-=-=
288 mov eax, DWORD [ebp-CMDSTR]
290 jne _repl_cmd_write_end
292 push DWORD [buffer_filename]
294 push DWORD [buffer_lines]
301 ; if no commands were matched, it's an error
302 jmp _repl_invalid_cmd
304 ; some error messages
327 %undef _BUFFER_FILENAME