starting on repl
[sped.git] / sped.asm
1 ; sped - the stupidly pointless editor
2 ; written by pinosaur
3
4 %include "fileutils.S"
5
6 global main
7 extern printf
8 extern fflush
9 extern stdout
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     nofile_str db `no file provided\n`, 0x00
23     prompt_str db `sped > `, 0x00
24     invalidcommand_str db `invalid command\n`, 0x00
25     charcount_str db `read %i chars\n`, 0x00
26
27 section .bss
28     buffer resb 4
29     buffer_lines resb 4
30     cur_line resb 4
31
32 section .text
33 main:
34     %define _ARGC 8
35     %define _ARGV 12
36
37     push ebp
38     mov ebp, esp
39
40     ; read command line args
41     mov ecx, [ebp+_ARGC]
42     cmp ecx, 1
43     jg _main_existing
44     
45     ; display error msg if no file
46     push nofile_str
47     call printf
48     mov eax, 1
49     jmp _main_exit
50
51     _main_existing:
52     mov ebx, DWORD [ebp+_ARGV]
53     add ebx, 4 ; first user arg is filename
54     push DWORD [ebx]
55     call readFile
56
57     mov [buffer], eax
58     mov [buffer_lines], ebx
59     mov DWORD [cur_line], 0x00
60
61     call repl
62
63     mov eax, 0
64     jmp _main_exit
65
66     _main_exit:
67     %undef _ARGC
68     %undef _ARGV
69
70     mov esp, ebp
71     pop ebp
72     ret
73
74 ; prompt for user
75 ; no args - reads from globals
76 repl:
77
78     %define CMDSTR 4 ; the previous line read from user
79
80     push ebp
81     mov ebp, esp
82
83     sub esp, 4
84
85     _repl_loop:
86     
87     ; print the prompt
88     push prompt_str
89     call printf
90     push DWORD [stdout]
91     call fflush
92
93     ; read line from stdin
94     push 0
95     call readLine
96
97     mov DWORD [ebp-CMDSTR], eax
98
99     ; commands are single char for now
100     cmp ecx, 1 
101     jne _repl_invalid
102
103     ; parse commands
104     mov eax, DWORD [ebp-CMDSTR]
105     mov eax, [eax]
106
107     ; q exists program
108     mov eax, DWORD [ebp-CMDSTR]
109     cmp BYTE [eax], 'q'
110     jne _repl_cmd_quit_end
111     jmp _repl_exit
112     _repl_cmd_quit_end:
113
114
115     _repl_invalid:
116     push invalidcommand_str
117     call printf
118
119     _repl_continue:
120     jmp _repl_loop
121     
122     _repl_exit:
123
124     %undef CMDSTR
125
126     mov esp, ebp
127     pop ebp
128     ret
129