This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
text-editor-main.asm
273 lines (236 loc) · 8.04 KB
/
text-editor-main.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
;;=============================================================================;;
;; ;;
;; Assembly Text Editor ;;
;; ;;
;; ;;
;; ;;
;; By Roduan Kareem Aldeen & Abdullhafiez Faraj ;;
;; ;;
;; ;;
;;=============================================================================;;
.stack 100h
.data
posX db 1 dup(0) ; dh = posX -> controls row
posY db 1 dup(0) ; dl = posY -> controls column
matrix db 80*25 dup(' ') ; 25 lines of 80 chars each.
curr_line dw ?
curr_char dw ?
color db 2*16+15
;FOR COLORS USE NEXT TABLE:
;http://stackoverflow.com/questions/29460318/how-to-print-colored-string-in-assembly-language/29478158#29478158
filename db "C:/file.txt",0
handler dw ?
length dw dup(0)
start_menu_str dw ' ',0ah,0dh
dw ' ',0ah,0dh
dw ' ',0ah,0dh
dw ' ',0ah,0dh
dw ' ====================================================',0ah,0dh
dw ' || ||',0ah,0dh
dw ' || * Assembly Text Editor * ||',0ah,0dh
dw ' || ||',0ah,0dh
dw ' ||--------------------------------------------------||',0ah,0dh
dw ' || ||',0ah,0dh
dw ' || ||',0ah,0dh
dw ' || ||',0ah,0dh
dw ' || Type in what you want, press ESC ||',0ah,0dh
dw ' || To exit the program. ||',0ah,0dh
dw ' || ||',0ah,0dh
dw ' || Press Enter to start ||',0ah,0dh
dw ' || ||',0ah,0dh
dw ' || ||',0ah,0dh
dw ' ====================================================',0ah,0dh
dw '$',0ah,0dh
.code
;INITIALIZE DATA SEGMENT.
mov ax,@data
mov ds,ax
call main_menu ;Print the main menu
start_prog:
call clear_screen
jmp program
program: ; Initalize the variables
mov curr_line, offset matrix
mov curr_char, 0
start:
call read_char
;;--------------------------------------------------------------------;;
;; ;;
;; Keys Listeners ;;
;; ;;
;;____________________________________________________________________;;
;DISPLAY LETTER, DIGIT OR ANY OTHER ACCEPTABLE CHAR.
any_char:
mov ah, 9
mov bh, 0
mov bl, color
mov cx, 1 ; how many times display char.
int 10h ; display char in al.
;UPDATE CHAR IN MATRIX.
mov si, curr_line ; si points to the beginning of the line.
add si, curr_char ; si points to the char in the line.
mov [ si ], al ; the char is in the matrix.
inc length ; count the number of chars
;!!! EXTREMELY IMPORTANT : PREVIOUS BLOCK DISPLAYS ONE
;CHAR, AND NEXT BLOCK MOVES CURSOR TO THE RIGHT. THAT'S
;THE NORMAL BEHAVIOR FOR ALL EDITORS. DO NOT MOVE THESE
;TWO BLOCKS, THEY MUST BE THIS WAY. IF IT'S NECESSARY
;TO MOVE THEM, ADD A JUMP FROM ONE BLOCK TO THE OTHER.
;RIGHT.
moveRight:
inc curr_char ; update current char.
mov dl, posX
mov dh, posY
inc dl ; posX ++
mov posX, dl
jmp prntCrs
;LEFT.
moveLeft:
dec curr_char ; update current char.
mov dl, posX
mov dh, posY
dec dl ; posX --
mov posX, dl
jmp prntCrs
;UP.
moveUp:
sub curr_line, 80 ; update current line.
mov dl, posX
mov dh, posY
dec dh ; posY --
mov posY, dh
jmp prntCrs ; print cursor
;DOWN.
moveDown:
add curr_line, 80 ; update current line.
mov dl, posX
mov dh, posY
inc dh ; posY ++
mov posY, dh
jmp prntCrs
;ENTER.
moveNewLine:
mov si, curr_line
add si, 79
mov [si], 0dh
add curr_line, 80
mov curr_char, 0
mov posX, 0
mov dl, posX
mov dh, posY
inc dh
mov posY, dh
add length, 80
jmp prntCrs
;HOME
moveToBeginning:
mov curr_char, 0
mov posX, 0
mov dl, posX
jmp prntCrs
backSpace:
;check if this is the first char in the line
cmp curr_char, 0
je preventBackSpace
;UPDATE CHAR IN MATRIX.
dec curr_char
mov si, curr_line ; si points to the beginning of the line.
add si, curr_char ; si points to the char in the line.
mov [ si ], ' ' ; the char is in the matrix.
dec length ; count the number of chars
dec posX
mov dl, posX
;Move the cursor
mov ah, 2h
int 10h
;Update the Screen
mov al,' '
mov ah, 9
mov bh, 0
mov bl, 0000
mov cx, 1 ; how many times display char.
int 10h ; display char in al.
jmp prntCrs
prntCrs: ; print cursor
mov ah, 2h
int 10h
jmp start ; Go back to the beginning
fin:
int 20h
saveToFile:
;CREATE FILE.
mov ah, 3ch
mov cx, 0
mov dx, offset filename
int 21h
;PRESERVE FILE HANDLER RETURNED.
mov handler, ax
;WRITE STRING.
mov ah, 40h
mov bx, handler
mov cx, length ;STRING LENGTH.
mov dx, offset matrix
int 21h
;CLOSE FILE (OR DATA WILL BE LOST).
mov ah, 3eh
mov bx, handler
int 21h
jmp fin
preventBackSpace:
call read_char
;;--------------------------------------------------------------------;;
;; ;;
;; Clear the sceen ;;
;; Just set new text mood for avoiding complexicity ;;
;; ;;
;;____________________________________________________________________;;
clear_screen proc near
mov ah,0 ;graphics mode
mov al,3 ;
int 10h
ret
clear_screen endp
main_menu proc
mov ah,09h
mov dh,0
mov dx, offset start_menu_str
int 21h
input: ;wait for ENTER KEY.
mov ah, 0
int 16h
cmp al, 27 ; ESC
je fin
cmp ax, 1C0Dh ; ENTER.
je start_prog
jmp input
main_menu endp
read_char proc
;CAPTURE KEY.
mov ah, 0
int 16h
;EVALUATE KEY.
cmp al, 27 ; ESC
je fin
cmp ax, 4800h ; UP.
je moveUp
cmp ax, 4B00h ; LEFT.
je moveLeft
cmp ax, 4D00H ; RIGHT.
je moveRight
cmp ax, 5000h ; DOWN.
je moveDown
cmp ax, 1C0Dh ; ENTER.
je moveNewLine
cmp ax, 4700h ; HOME.
je moveToBeginning
cmp ax, 3F00h ; F5.
je saveToFile
cmp ax, 0E08h ; BackSpace.
je backSpace
cmp al, 32
jae any_char
jmp start
read_char endp
get_file_location_from_user proc
ret
get_file_location_from_user endp