abstracting
[sped.git] / sped.asm
1 ; sped - the stupidly pointless editor
2 ; written by pinosaur
3
4 %include "fileutils.S"
5 %include "repl.S"
6
7 extern printf
8
9 global main
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     readlines_str db `opened file with %i lines\n`, 0x00
24
25 section .text
26 main:
27     %define _ARGC 8
28     %define _ARGV 12
29
30     %define BUFFER          4
31     %define BUFFER_LINES    8
32     %define BUFFER_FILENAME 12
33
34     push ebp
35     mov ebp, esp
36
37     sub esp, 12
38
39     ; read command line args
40     mov ecx, [ebp+_ARGC]
41     cmp ecx, 1
42     jg _main_existing
43     
44     ; display error msg if no file
45     push nofile_str
46     call printf
47     mov eax, 1
48     jmp _main_exit
49
50     _main_existing:
51     mov ebx, DWORD [ebp+_ARGV]
52     add ebx, 4 ; first user arg is filename
53     mov ebx, [ebx]
54     mov [ebp-BUFFER_FILENAME], ebx
55
56     push DWORD [ebp-BUFFER_FILENAME]
57     call readFile
58
59     mov [ebp-BUFFER], eax
60     mov [ebp-BUFFER_LINES], ecx
61
62     push DWORD [ebp-BUFFER_LINES]
63     push readlines_str
64     call printf
65
66     push DWORD [ebp-BUFFER]
67     push DWORD [ebp-BUFFER_LINES]
68     push DWORD [ebp-BUFFER_FILENAME]
69     call repl
70
71     mov eax, 0
72     jmp _main_exit
73
74     _main_exit:
75
76     ; free string array
77
78     %undef _ARGC
79     %undef _ARGV
80     %undef BUFFER
81     %undef BUFFER_LINES
82     %undef BUFFER_FILENAME
83
84     mov esp, ebp
85     pop ebp
86     ret
87