-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.asm
More file actions
1007 lines (978 loc) · 20.4 KB
/
source.asm
File metadata and controls
1007 lines (978 loc) · 20.4 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
locals
jumps
; Sets cursor position in the default video page to the
; current data in the memory.
setCursorToCurrData macro
mov ah, 2
xor bh, bh
mov dl, cursorX
mov dh, cursorY
int 10h
endm
; Runs entire process of creating and opening a new (empty) file.
completeNewFile macro
call CheckSave
call SetFileName
push offset fileName
call CreateFile
call OpenFile
call DeleteMessageBuffer
call ClearScreen
push offset menuType offset menuColor 0
call PrintBar
mov cursorX, 0
mov cursorY, 2
setCursorToCurrData
endm
; Runs entire process of saving to a file.
completeSaveFile macro
push offset menuMenu offset mainFileHighC 0
call PrintBar
push offset fileMenu offset fileMenuSaveHigh 1
call PrintBar
setCursorToCurrData
call SetFileName
push offset fileName
call CreateFile
call OpenFile
call WriteToFile
call ClearScreen
call PrintMessage
endm
; Runs entire process of opening and reading an existing file.
completeOpenReadFile macro
push offset menuType offset mainFileHighC 0
call PrintBar
push offset fileMenu offset fileMenuOpenHigh 1
call PrintBar
setCursorToCurrData
call SetFileName
call OpenFile
call ReadFile
call ClearScreen
call PrintMessage
endm
setDataSegment macro dseg
push ax
mov ax, dseg
mov ds, ax
assume ds:dseg
pop ax
endm
data segment
fileName db 22, 23 dup(?)
fileHandle dw ?
uErrorMsg db "UNKNOWN ERROR", 10, 13, "$"
errorMsg2 db "FILE NOT FOUND", 10, 13, "$"
errorMsg3 db "PATH NOT FOUND", 10, 13, "$"
errorMsg5 db "ACCESS DENIED", 10, 13, "$"
errorMsg12 db "ACCESS CODE INVALID", 10, 13, "$"
pressAnyKey db "Press any key to continue...", 10, 13, "$"
askForName db 10, 13, "Filename: ", "$"
checkSaveS db 10, 13, "Do you want to save before quitting? (Y/N) ", "$"
checkSaveE db 10, 13, "INVALID INPUT, try again!", "$"
menuType db " File Edit Help", 5 dup(20h), "Opened: ", 45 dup(20h), "TYPE "
menuMenu db " File Edit Help", 5 dup(20h), "Opened: ", 45 dup(20h), "MENU "
menuColor db 77h, 74h, 3 dup(70h), 2 dup(77h), 74h, 3 dup(70h), 2 dup(77h), 74h, 3 dup(70h), 5 dup(77h), 8 dup(70h), 44 dup(72h), 6 dup(2Fh)
mainFileHighC db 37h, 34h, 3 dup(30h), 37h, 77h, 74h, 3 dup(70h), 2 dup(77h), 74h, 3 dup(70h), 5 dup(77h), 8 dup(70h), 44 dup(72h), 6 dup(2Fh)
mainEditHighC db 77h, 74h, 3 dup(70h), 77h, 37h, 34h, 3 dup(30h), 37h, 77h, 74h, 3 dup(70h), 5 dup(77h), 8 dup(70h), 44 dup(72h), 6 dup(2Fh)
mainHelpHighC db 77h, 74h, 3 dup(70h), 77h, 77h, 74h, 3 dup(70h), 77h, 37h, 34h, 3 dup(30h), 37h, 4 dup(77h), 8 dup(70h), 44 dup(72h), 6 dup(2Fh)
fileMenu db " New (Alt+N) Open (Alt+O) Save (Alt+S)", 40 dup(20h)
fileMenuNewHigh db 6 dup(30h), 5 dup(34h), 2 dup(30h), 7 dup(70h), 5 dup(74h), 9 dup(70h), 5 dup(74h), 41 dup(70h)
fileMenuOpenHigh db 6 dup(70h), 5 dup(74h), 2 dup(70h), 7 dup(30h), 5 dup(34h), 2 dup(30h), 7 dup(70h), 5 dup(74h), 41 dup(70h)
fileMenuSaveHigh db 6 dup(70h), 5 dup(74h), 9 dup(70h), 5 dup(74h), 2 dup(70h), 7 dup(30h), 5 dup(34h), 2 dup(30h), 40 dup(70h)
editMenu db " Delete all text (Alt+Del)", 54 dup(20h)
editMenuDeleteHigh db 18 dup(30h), 7 dup(34h), 2 dup(30h), 53 dup(70h)
helpMenu db " Help (F1) About (F2)", 58 dup(20h)
helpMenuF1High db 7 dup(30h), 2 dup(34h), 2 dup(30h), 8 dup(70h), 2 dup(74h), 59 dup(70h)
helpMenuF2High db 7 dup(70h), 2 dup(74h), 2 dup(70h), 8 dup(30h), 2 dup(34h), 2 dup(30h), 58 dup(70h)
textBlanks db 80 dup(20h)
colorBlanks db 80 dup(0)
aboutString db 35 dup(20h), "Notepad--", 10, 13, 22 dup(20h), "The open source x86 ASM text editor!", 10, 13, 23 dup(20h), "Created by Barak Nehmad, 2015-2016", 10, 13, 27 dup(20h), "Press any key to return...$"
helpString db "========================================", 10, 13, "Keyboard shortcuts:", 10, 13, "Alt+S = Save current buffer to file", 10, 13, "Alt+O = Open a file", 10, 13, "Alt+N = Create new blank buffer", 10, 13, "Alt+X OR Alt+F4 = Exit", 10, 13, "F1 = Open help page (this one!)", 10, 13, "F2 = Open about page", 10, 13, "F10 = Enter menu navigation mode", 10, 13, "========================================", 10, 13, "Navigation tips:", 10, 13, "When in menu navigation mode, press the left and right arrow keys to navigate", 10, 13, "within the current selected sub-menu. In order to move to another sub-menu,", 10, 13, "press Alt+right/left arrow keys.", 10, 13, "In order to return to file editing, press ESC.$"
messagePos dw 0
numOfReadBytes dw 0
cursorX db 0
cursorY db 0
txtWild db "*.TXT"
pyWild db "*.PY"
batchWild db "*.BAT"
asmWild db "*.ASM"
data ends
; Second data segment dedicated for a large message buffer which allows for 65535 bytes of data.
mesDat segment
message db 0FFFFh dup(?)
mesDat ends
stac segment stack
dw 100h dup(?)
stac ends
code segment
assume cs:code, ds:data, ss:stac
ClearScreen proc
push ax bx cx dx ; Push regs to stack
pushf ; Push flags to stack
mov cx, 25
mov ah, 2
mov dl, 10
@@Clear:
int 21h
loop @@Clear
xor dl, dl
xor dh, dh
xor bh, bh
mov ah, 2
int 10h
popf ; Pop flags from stack
pop dx cx bx ax ; Pop regs from stack
ret
ClearScreen endp
; Gets called whenever there's an error, displays error message.
; Uses AX to determine which error message to display.
ErrorMessages proc
push ax cx dx di es
pushf
cmp ax, 2
jz @@FileNotFound
cmp ax, 3
jz @@PathDoesNotExist
cmp ax, 5
jz @@AccessDenied
cmp ax, 12
jz @@AccessCodeInvalid
jmp @@UnknownError
@@FileNotFound:
lea dx, errorMsg2
jmp @@ShowError
@@PathDoesNotExist:
lea dx, errorMsg3
jmp @@ShowError
@@AccessDenied:
lea dx, errorMsg5
jmp @@ShowError
@@AccessCodeInvalid:
lea dx, errorMsg12
jmp @@ShowError
@@UnknownError:
lea dx, uErrorMsg
@@ShowError:
mov ah, 9
int 21h
lea dx, pressAnyKey
int 21h
mov ah, 7
int 21h
@@Reset:
call ClearScreen
mov ax, data
mov es, ax
lea di, fileName
inc di
xor al, al
cld
mov cx, 22
rep stosb
mov fileHandle, 0
call PrintMessage
@@Return:
popf
pop es di dx cx ax
ret
ErrorMessages endp
; Prints menu bar.
; Recieves three parameters through stack:
; TextParameter - array of 80 bytes which contains textual data to print.
; MenuColorParameter - array of 80 bytes which contains text color attributes.
; RowToPrint - type int. This determines where to print the menu bar.
TextParameter equ [bp+8]
MenuColorParameter equ [bp+6]
RowToPrint equ [bp+4]
PrintBar proc
push bp
mov bp, sp
push ax bx cx dx si
pushf
mov dh, RowToPrint
xor dl, dl
xor bh, bh
mov ah, 2
int 10h
xor si, si
mov cx, 1
@@Print:
mov ah, 9
mov bx, TextParameter
mov al, [bx+si]
mov bx, MenuColorParameter
mov bl, [bx+si]
xor bh, bh
int 10h
mov ah, 2
inc dl
int 10h
inc si
cmp si, 80
jc @@Print
mov ah, 2
xor bh, bh
xor dl, dl
mov dh, 2
int 10h
popf
pop si dx cx bx ax
pop bp
ret 6
PrintBar endp
; Creates a file according to passed pathname.
; Recieves one parameter through the stack:
; CreateFile - Pathname in ASCII.
FileToCreate equ [bp+4]
CreateFile proc
push bp
mov bp, sp
push ax cx dx
pushf
mov dx, FileToCreate
xor cx, cx
mov ah, 3Ch
int 21h
mov fileHandle, ax
popf
pop dx cx ax
pop bp
ret 2
CreateFile endp
; Asks user for a filename and pushes answer to memory after cleaning it up.
SetFileName proc
push ax bx cx dx si di
pushf
lea dx, askForName
mov ah, 9
int 21h
lea dx, fileName
mov bx, 21
mov ah, 0Ah
int 21h
mov cl, fileName[1]
xor ch, ch
; Shift moves each letter after the first two ones two bytes to the left.
; This is because the first two bytes are taken by information that is
; not needed and disturbs reading the file name.
mov si, 2
@@Shift:
mov al, fileName[si]
sub si, 2
mov fileName[si], al
add si, 3
cmp si, 21
jc @@Shift
; End of Shift label, these lines remove the enter ASCII code
; at the end of the string.
mov si, cx
mov fileName[si], 0
push cx
@@PrintOnBar:
mov ax, data
mov es, ax
lea di, menuType
add di, 30
xor al, al
cld
mov cx, 22
rep stosb
lea di, menuMenu
add di, 30
mov cx, 22
rep stosb
pop cx
push cx
lea si, fileName
lea di, menuType
add di, 30
rep movsb
pop cx
lea si, fileName
lea di, menuMenu
add di, 30
rep movsb
popf
pop di si dx cx bx ax
ret
SetFileName endp
; Opens file using filename from memory.
OpenFile proc
push ax dx ds
pushf
setDataSegement data
lea dx, fileName
mov ah, 3Dh
mov al, 2
int 21h
jc @@OpenError
mov fileHandle, ax
popf
pop ds dx ax
ret
@@OpenError:
call ErrorMessages
popf
pop ds dx ax
ret
OpenFile endp
; Reads opened file into message buffer.
ReadFile proc
push ax bx cx dx si ds
pushf
call DeleteMessageBuffer
mov bx, fileHandle
mov cx, 0FFFFh
setDataSegement mesDat
lea dx, message
mov ah, 3Fh
int 21h
setDataSegement data
mov numOfReadBytes, ax
mov messagePos, ax
mov si, ax
setDataSegement mesDat
mov message[si], '$' ; Adds a terminate to the end of the string.
setDataSegement data
popf
pop ds si dx cx bx ax
ret
ReadFile endp
; Gets current position of file pointer and returns it to memory.
GetFilePos proc
push ax bx cx dx
pushf
mov ah, 42h ; seek file pointer (same as file.seek() in python)
mov al, 1 ; current location plus offset
mov bx, fileHandle
xor cx, cx ; high order word of bytes to move
xor dx, dx ; low order word of bytes to move
int 21h ; new pointer stored in DX:AX
mov messagePos, ax
popf
pop dx cx bx ax
ret
GetFilePos endp
; Prints buffer to screen.
PrintMessage proc
push ax bx dx ds
pushf
mov ah, 2
xor bh, bh
mov dh, 2
xor dl, dl
int 10h
setDataSegement data
push si bx
mov si, numOfReadBytes
mov bx, messagePos
setDataSegement mesDat
cmp si, bx
jz @@PosEquRead
mov message[si], 0
mov message[bx], '$'
@@PosEquRead:
pop bx si
mov ah, 9
lea dx, message
int 21h
setDataSegement data
mov ah, 3
int 10h
mov cursorX, dl
mov cursorY, dh
push offset menuType offset menuColor 0
call PrintBar
push offset textBlanks offset colorBlanks 1
call PrintBar
setCursorToCurrData
popf
pop ds dx bx ax
ret
PrintMessage endp
DeleteMessageBuffer proc
push ax cx di es
pushf
mov cx, messagePos
mov ax, mesDat
mov es, ax
lea di, message
mov al, 0
cld
rep stosb
mov messagePos, 0
call ClearScreen
push offset menuType offset menuColor 0
call PrintBar
push offset textBlanks offset colorBlanks 1
call PrintBar
call GetCursorPos
popf
pop es di cx ax
ret
DeleteMessageBuffer endp
; Writes to file using handle and message buffer.
WriteToFile proc
push ax bx cx dx ds
pushf
setDataSegement data
mov bx, fileHandle
mov cx, messagePos
setDataSegement mesDat
lea dx, message
mov ah, 40h
int 21h
jc @@Error
mov si, cx
mov message[si], '$'
popf
pop ds dx cx bx ax
ret
@@Error:
call ErrorMessages
popf
pop ds dx cx bx ax
ret
WriteToFile endp
; Closes file and frees file handle.
CloseFile proc
push ax bx ds
pushf
setDataSegement data
mov bx, fileHandle
mov ah, 3Eh
int 21h
popf
pop ds bx ax
ret
CloseFile endp
; Deletes a file according to passed pathname.
; Recieves one parameter through the stack:
; FileToDelete - Pathname in ASCII.
FileToDelete equ [bp+4]
DeleteFile proc
push bp
mov bp, sp
push ax dx
pushf
mov ah, 41h
mov dx, FileToDelete ; pointer to ascii file name to delete
int 21h
jnc @@EndProc ; if CF set, error code is in AX
call ErrorMessages
@@EndProc:
popf
pop dx ax
pop bp
ret 2
DeleteFile endp
; Main input loop. Gets input from user and writes it to message buffer.
MainInput proc
jmp @@GetKey
@@Enter:
mov si, messagePos
add messagePos, 2
setDataSegement mesDat
mov message[si], 13
inc si
mov message[si], 10
jmp @@GetKey
@@Backspace:
call Backspace
jmp @@GetKey
@@Write:
setDataSegement data
mov si, messagePos
inc messagePos
setDataSegement mesDat
mov message[si], al
@@GetKey:
setDataSegement data
call GetCursorPos
mov ah, 1
int 21h
cmp al, 8
jz @@Backspace
cmp cursorY, 24
jz @@TestReprintBars
cmp al, 13
jz @@Enter
cmp al, 0
jnz @@Write
call RecognizeDoubleKey
jmp @@GetKey
@@TestReprintBars:
cmp al, 13
jz @@BarsEnter
cmp al, 0
jnz @@BarsWrite
call RecognizeDoubleKey
jmp @@GetKey
@@BarsEnter:
mov si, messagePos
add messagePos, 2
setDataSegement mesDat
mov message[si], 13
inc si
mov message[si], 10
jmp @@ReprintBars
@@BarsWrite:
setDataSegement data
mov si, messagePos
inc messagePos
setDataSegement mesDat
mov message[si], al
setDataSegement data
cmp cursorX, 79
jnz @@ReturnWithoutReprint
@@ReprintBars:
setDataSegement data
push offset menuType offset menuColor 0
call PrintBar
push offset textBlanks offset colorBlanks 1
call PrintBar
xor dl, dl
mov dh, 24
xor bh, bh
mov ah, 2
int 10h
jmp @@GetKey
@@ReturnWithoutReprint:
jmp @@GetKey
ret
MainInput endp
; Handles backspace press.
Backspace proc
push ax bx cx dx si di ds es
pushf
cmp messagePos, 0
jz @@EndProc
dec messagePos
mov si, messagePos
setDataSegement mesDat
mov message[si], 0
setDataSegement data
cmp cursorX, 0
ja @@DelLetter
cmp cursorY, 2
jz @@ReprintEnterCheck
dec si
setDataSegement mesDat
cmp message[si], 13
jz @@EnterFound
setDataSegement data
mov dl, 79
mov dh, cursorY
dec dh
xor bh, bh
mov ah, 2
int 10h
jmp @@DelLetter
@@ReprintEnterCheck:
dec si
setDataSegement mesDat
cmp message[si], 13
jnz @@CallPrint
mov message[si], 0
setDataSegement data
dec messagePos
@@CallPrint:
call PrintMessage
jmp @@DelLetter
@@EnterFound:
dec si
mov ax, mesDat
mov es, ax
assume es:mesDat
lea di, message
add di, si
std
mov al, 10
mov cx, si
repnz scasb
cmp di, 0
jz @@DiIsStartOfDoc
inc di
jmp @@Division
@@DiIsStartOfDoc:
inc si
@@Division:
sub si, di
mov ax, si
xor dx, dx
mov bx, 80
div bx
xor bh, bh
setDataSegement data
mov dh, cursorY
dec dh
mov ah, 2
int 10h
dec messagePos
mov si, messagePos
setDataSegement mesDat
mov message[si], 0
setDataSegement data
@@DelLetter:
mov ah, 0Ah
xor al, al
xor bh, bh
mov cx, 1
int 10h
@@EndProc:
popf
pop es ds di si dx cx bx ax
ret
Backspace endp
; Queries cursor position and returns data to memory.
GetCursorPos proc
push ax bx dx
pushf
setDataSegement data
mov ah, 3
xor bh, bh
int 10h
mov cursorX, dl
mov cursorY, dh
popf
pop dx bx ax
ret
GetCursorPos endp
; Uses data from previous int 21h|7 in order to determine a keyboard shortcut.
RecognizeDoubleKey proc
push ax bx dx
pushf
mov ah, 7
int 21h
cmp al, 31h ; alt+n
jz @@NewFile
cmp al, 1Fh ; alt+s
jz @@SaveFile
cmp al, 18h ; alt+o
jz @@OpenFile
cmp al, 0A3h ;alt+del
jz @@DeleteBuffer
cmp al, 3Bh ; F1
jz @@Help
cmp al, 3Ch ; F2
jz @@About
cmp al, 21h ; alt+f
jz @@FileMenu
cmp al, 12h ; alt+e
jz @@EditMenu
cmp al, 23h ; alt+h
jz @@HelpMenu
cmp al, 44h ; F10
jz @@MenuMode
cmp al, 2Dh ; alt+x
jz @@Exit
cmp al, 6Bh ; alt+F4
jz @@Exit
setCursorToCurrData
jmp @@Return
@@NewFile:
completeNewFile
jmp @@Return
@@SaveFile:
completeSaveFile
jmp @@Return
@@OpenFile:
completeOpenReadFile
jmp @@Return
@@DeleteBuffer:
call DeleteMessageBuffer
jmp @@Return
@@Help:
push offset helpString 0
call PrintSecondPage
jmp @@Return
@@About:
push offset aboutString 7
call PrintSecondPage
jmp @@Return
@@FileMenu:
push 0
call MenuMode
push offset menuType offset menuColor 0
call PrintBar
push offset textBlanks offset colorBlanks 1
call PrintBar
setCursorToCurrData
jmp @@Return
@@EditMenu:
push 1
call MenuMode
push offset menuType offset menuColor 0
call PrintBar
push offset textBlanks offset colorBlanks 1
call PrintBar
setCursorToCurrData
jmp @@Return
@@HelpMenu:
push 2
call MenuMode
push offset menuType offset menuColor 0
call PrintBar
push offset textBlanks offset colorBlanks 1
call PrintBar
setCursorToCurrData
jmp @@Return
@@MenuMode:
push 0
call MenuMode
push offset menuType offset menuColor 0
call PrintBar
push offset textBlanks offset colorBlanks 1
call PrintBar
setCursorToCurrData
@@Return:
popf
pop dx bx ax
ret
@@Exit:
call CheckSave
call CloseFile
mov ah, 4Ch
int 21h
RecognizeDoubleKey endp
DefaultMenu equ [bp+4]
MenuMode proc
push bp
mov bp, sp
push ax bx
pushf
mov bh, DefaultMenu ; bh is selected sub-menu
mov bl, 0 ; bl is selected entry in sub-menu
@@CheckBH:
cmp bh, 1
jc @@FileMenuCheck ; bh is 0
ja @@HelpMenuCheck ; bh is 2
@@EditMenuCheck:
push offset menuMenu offset mainEditHighC 0
call PrintBar
push offset editMenu offset editMenuDeleteHigh 1
call PrintBar
jmp @@StartInput
@@FileMenuCheck:
push offset menuMenu offset mainFileHighC 0
call PrintBar
cmp bl, 1
jc @@FileMenuBLIs0
ja @@FileMenuBLIs2
push offset fileMenu offset fileMenuOpenHigh 1
call PrintBar
jmp @@StartInput
@@FileMenuBLIs0:
push offset fileMenu offset fileMenuNewHigh 1
call PrintBar
jmp @@StartInput
@@FileMenuBLIs2:
push offset fileMenu offset fileMenuSaveHigh 1
call PrintBar
jmp @@StartInput
@@HelpMenuCheck:
push offset menuMenu offset mainHelpHighC 0
call PrintBar
cmp bl, 0
jnz @@HelpMenuBLIs1
push offset helpMenu offset helpMenuF1High 1
call PrintBar
jmp @@StartInput
@@HelpMenuBLIs1:
push offset helpMenu offset helpMenuF2High 1
call PrintBar
@@StartInput:
mov ah, 7
int 21h
cmp al, 13 ; enter
jz @@Select
cmp al, 1Bh ; escape
jz @@Return
cmp al, 0
jnz @@StartInput
int 21h
cmp al, 4Dh ; right
jz @@MoveInSubRight
cmp al, 4Bh ; left
jz @@MoveInSubLeft
cmp al, 9Dh ; alt+right
jz @@MoveSubRight
cmp al, 9Bh ; alt+left
jz @@MoveSubLeft
jmp @@StartInput
@@MoveInSubRight:
cmp bh, 1
jc @@MoveInSubFileRight
ja @@MoveInSubHelpRight
@@MoveInSubEditRight:
jmp @@EditMenuCheck
@@MoveInSubFileRight:
cmp bl, 2
jz @@MaxBL
inc bl
jmp @@FileMenuCheck
@@MoveInSubHelpRight:
cmp bl, 1
jz @@MaxBL
inc bl
jmp @@HelpMenuBLIs1
@@MaxBL:
xor bl, bl
jmp @@CheckBH
@@MoveInSubLeft:
cmp bh, 1
jc @@MoveInSubFileLeft
ja @@MoveInSubHelpLeft
@@MoveInSubEditLeft:
jmp @@EditMenuCheck
@@MoveInSubFileLeft:
cmp bl, 0
jz @@MinBL2
dec bl
jmp @@FileMenuCheck
@@MoveInSubHelpLeft:
cmp bl, 0
jz @@MinBL1
dec bl
jmp @@HelpMenuCheck
@@MinBL2:
mov bl, 2
jmp @@CheckBH
@@MinBL1:
inc bl
jmp @@CheckBH
@@MoveSubRight:
cmp bh, 1
ja @@MaxBH
inc bh
jmp @@CheckBH
@@MaxBH:
xor bh, bh
jmp @@CheckBH
@@MoveSubLeft:
cmp bh, 1
jc @@MinBH
dec bh
jmp @@CheckBH
@@MinBH:
mov bh, 2
jmp @@CheckBH
@@Select:
cmp bh, 1
jc @@SelectInFile
ja @@SelectInHelp
@@SelectInEdit:
@@InEditSelectDelete:
call DeleteMessageBuffer
jmp @@Return
@@SelectInFile:
cmp bl, 1
jc @@InFileSelectNew
ja @@InFileSelectSave
@@InFileSelectOpen:
completeOpenReadFile
jmp @@Return
@@InFileSelectNew:
completeNewFile
jmp @@Return
@@InFileSelectSave:
completeSaveFile
jmp @@Return
@@SelectInHelp:
cmp bl, 1
jz @@InHelpSelectAbout
@@InHelpSelectHelp:
push offset helpString 0
call PrintSecondPage
jmp @@Return
@@InHelpSelectAbout:
push offset aboutString 7
call PrintSecondPage
jmp @@Return
@@Return:
popf
pop bx ax
pop bp
ret 2
MenuMode endp
StringToPrint equ [bp+6]
RowToPrint equ [bp+4]
PrintSecondPage proc
push bp
mov bp, sp
push ax cx dx si
pushf
mov ah, 5
mov al, 1
int 10h
call ClearScreen
mov ah, 2
mov bh, 1
mov dh, RowToPrint
xor dl, dl
int 10h
mov ah, 9
mov dx, StringToPrint
int 21h
mov ah, 7
int 21h
mov ah, 5
xor al, al
int 10h
setCursorToCurrData
popf
pop si dx cx ax
pop bp
ret 4
PrintSecondPage endp
; Upon request of exit, asks user whether to save the file or not.
CheckSave proc
push ax dx
pushf
@@PrintStart:
mov ah, 9
lea dx, checkSaveS
int 21h
@@GetInput:
mov ah, 1
int 21h
cmp al, 79h ; lowCase y
jz @@Yes
cmp al, 59h ; caps Y
jz @@Yes
cmp al, 6Eh ; lowCase n
jz @@No
cmp al, 4Eh ; caps N
jz @@No
mov ah, 9
lea dx, checkSaveE
int 21h
jmp @@PrintStart
@@Yes:
call SetFileName
push offset fileName
call CreateFile
call OpenFile
call WriteToFile
popf
pop dx ax
ret
@@No:
popf
pop dx ax
ret
CheckSave endp
Start:
setDataSegement data
call ClearScreen
push offset menuType offset menuColor 0
call PrintBar