-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.asm
executable file
·1414 lines (1142 loc) · 20.1 KB
/
kernel.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
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
[BITS 16]
org 0000h
mov ax, cs
mov ds, ax
section .text
global _start
_start:
;set text video mode
mov AH, 00h
mov AL, 02h
int 10h
; to clear the keyboard buffer
; there is an Enter which comes from welcome
call readkey
call printcmdsymb
call mainloop
ret
mainloop:
call readkey
call checkkey
jmp mainloop
ret
checkkey:
;check if Enter has been pressed
cmp AH, 1cH
je checkkey_enter
;check if bksp has been pressed
cmp AH, 0eH
je checkkey_bksp
;check if up key has been pressed
cmp AH, 48H
je checkkey_fin
;check if right key has been pressed
cmp AH, 4dH
je checkkey_fin
;check if down key has been pressed
cmp AH, 50H
je checkkey_scrollup
;check if left key has been pressed
cmp AH, 4bH
je checkkey_fin
;check if 256'th char has been reached
mov DL, [cmdlen]
cmp DL, 0xFF
je checkkey_fin
call printchar
call savechar
jmp checkkey_fin
checkkey_bksp:
call bksp
jmp checkkey_fin
checkkey_enter:
call enter
jmp checkkey_fin
checkkey_scrollup:
call ScrollUp
jmp checkkey_fin
checkkey_fin:
ret
enter:
mov byte [wasClear], 0
call newline
call CheckCMD
cmp byte [wasClear], 1
je enter_skip_new_line
call newline
enter_skip_new_line:
call printcmdsymb
ret
PrintUnknownCMD:
mov AH, 0eh
mov BL, 0
mov AL, "c"
int 10h
mov AL, "o"
int 10h
mov AL, "m"
int 10h
mov AL, "m"
int 10h
mov AL, "a"
int 10h
mov AL, "n"
int 10h
mov AL, "d"
int 10h
mov AL, " "
int 10h
mov CL, 0
mov BX, cmd
PrintUnknowkCMD_loop:
mov AL, [BX]
int 10H
inc BX
inc CL
cmp CL, byte [cmdlen]
jne PrintUnknowkCMD_loop
mov AL, " "
int 10h
mov AL, "u"
int 10h
mov AL, "n"
int 10h
mov AL, "k"
int 10h
mov AL, "n"
int 10h
mov AL, "o"
int 10h
mov AL, "w"
int 10h
mov AL, "n"
int 10h
ret
CheckCMD:
cmp byte [cmdlen], 0
je CheckCMD_fin
mov byte [known], 0
call CheckAbout
cmp byte [known], 1
je CheckCMD_fin
call CheckAscii
cmp byte [known], 1
je CheckCMD_fin
call CheckHelp
cmp byte [known], 1
je CheckCMD_fin
call CheckClear
cmp byte [known], 1
je CheckCMD_fin
call CheckReboot
cmp byte [known], 1
je CheckCMD_fin
call CheckBeep
cmp byte [known], 1
je CheckCMD_fin
call CheckWriteFLP
cmp byte [known], 1
je CheckCMD_fin
call CheckReadFLP
cmp byte [known], 1
je CheckCMD_fin
call CheckChrono
cmp byte [known], 1
je CheckCMD_fin
call CheckFib
cmp byte [known], 1
je CheckCMD_fin
call CheckDraw
cmp byte [known], 1
je CheckCMD_fin
;if not recognized print "unknown command"
mov AH, 1
cmp AH, [known]
je CheckCMD_fin
call PrintUnknownCMD
CheckCMD_fin:
mov byte [cmdlen], 00H ;reset cmdlen to 0
ret
CheckAbout:
cmp byte [cmdlen], 5
jne CheckAbout_fin
mov BX, cmd
cmp byte [BX], "a"
jne CheckAbout_fin
inc BX
cmp byte [BX], "b"
jne CheckAbout_fin
inc BX
cmp byte [BX], "o"
jne CheckAbout_fin
inc BX
cmp byte [BX], "u"
jne CheckAbout_fin
inc BX
cmp byte [BX], "t"
jne CheckAbout_fin
call PerformAbout
CheckAbout_fin:
ret
CheckAscii:
cmp byte [cmdlen], 5
jne CheckAscii_fin
mov BX, cmd
cmp byte [BX], "a"
jne CheckAscii_fin
inc BX
cmp byte [BX], "s"
jne CheckAscii_fin
inc BX
cmp byte [BX], "c"
jne CheckAscii_fin
inc BX
cmp byte [BX], "i"
jne CheckAscii_fin
inc BX
cmp byte [BX], "i"
jne CheckAscii_fin
call PerformAscii
CheckAscii_fin:
ret
CheckHelp:
cmp byte [cmdlen], 4
jne CheckHelp_fin
mov BX, cmd
cmp byte [BX], "h"
jne CheckHelp_fin
inc BX
cmp byte [BX], "e"
jne CheckHelp_fin
inc BX
cmp byte [BX], "l"
jne CheckHelp_fin
inc BX
cmp byte [BX], "p"
jne CheckHelp_fin
call PerformHelp
CheckHelp_fin:
ret
CheckClear:
cmp byte [cmdlen], 5
jne CheckClear_fin
mov BX, cmd
cmp byte [BX], "c"
jne CheckClear_fin
inc BX
cmp byte [BX], "l"
jne CheckClear_fin
inc BX
cmp byte [BX], "e"
jne CheckClear_fin
inc BX
cmp byte [BX], "a"
jne CheckClear_fin
inc BX
cmp byte [BX], "r"
jne CheckClear_fin
call PerformClear
CheckClear_fin:
ret
CheckReboot:
cmp byte [cmdlen], 6
jne CheckReboot_fin
mov BX, cmd
cmp byte [BX], "r"
jne CheckReboot_fin
inc BX
cmp byte [BX], "e"
jne CheckReboot_fin
inc BX
cmp byte [BX], "b"
jne CheckReboot_fin
inc BX
cmp byte [BX], "o"
jne CheckReboot_fin
inc BX
cmp byte [BX], "o"
jne CheckReboot_fin
inc BX
cmp byte [BX], "t"
jne CheckReboot_fin
call PerformReboot
CheckReboot_fin:
ret
CheckBeep:
cmp byte [cmdlen], 4
jne CheckBeep_fin
mov BX, cmd
cmp byte [BX], "b"
jne CheckBeep_fin
inc BX
cmp byte [BX], "e"
jne CheckBeep_fin
inc BX
cmp byte [BX], "e"
jne CheckBeep_fin
inc BX
cmp byte [BX], "p"
jne CheckBeep_fin
call PerformBeep
CheckBeep_fin:
ret
CheckWriteFLP:
mov BX, cmd
cmp byte [BX], "w"
jne CheckWriteFLP_fin
inc BX
cmp byte [BX], "r"
jne CheckWriteFLP_fin
inc BX
cmp byte [BX], "i"
jne CheckWriteFLP_fin
inc BX
cmp byte [BX], "t"
jne CheckWriteFLP_fin
inc BX
cmp byte [BX], "e"
jne CheckWriteFLP_fin
inc BX
cmp byte [BX], "f"
jne CheckWriteFLP_fin
inc BX
cmp byte [BX], "l"
jne CheckWriteFLP_fin
inc BX
cmp byte [BX], "p"
jne CheckWriteFLP_fin
call PerformWriteFLP
CheckWriteFLP_fin:
ret
CheckReadFLP:
mov BX, cmd
cmp byte [BX], "r"
jne CheckReadFLP_fin
inc BX
cmp byte [BX], "e"
jne CheckReadFLP_fin
inc BX
cmp byte [BX], "a"
jne CheckReadFLP_fin
inc BX
cmp byte [BX], "d"
jne CheckReadFLP_fin
inc BX
cmp byte [BX], "f"
jne CheckReadFLP_fin
inc BX
cmp byte [BX], "l"
jne CheckReadFLP_fin
inc BX
cmp byte [BX], "p"
jne CheckReadFLP_fin
call PerformReadFLP
CheckReadFLP_fin:
ret
CheckChrono:
mov BX, cmd
cmp byte [BX], "c"
jne CheckChrono_fin
inc BX
cmp byte [BX], "h"
jne CheckChrono_fin
inc BX
cmp byte [BX], "r"
jne CheckChrono_fin
inc BX
cmp byte [BX], "o"
jne CheckChrono_fin
inc BX
cmp byte [BX], "n"
jne CheckChrono_fin
inc BX
cmp byte [BX], "o"
jne CheckChrono_fin
call PerformChrono
CheckChrono_fin:
ret
CheckFib:
mov BX, cmd
cmp byte [BX], "f"
jne CheckFib_fin
inc BX
cmp byte [BX], "i"
jne CheckFib_fin
inc BX
cmp byte [BX], "b"
jne CheckFib_fin
inc BX
cmp byte [BX], " "
jne CheckFib_fin
call PerformFib
CheckFib_fin:
ret
CheckDraw:
mov BX, cmd
cmp byte [BX], "d"
jne CheckDraw_fin
inc BX
cmp byte [BX], "r"
jne CheckDraw_fin
inc BX
cmp byte [BX], "a"
jne CheckDraw_fin
inc BX
cmp byte [BX], "w"
jne CheckDraw_fin
call PerformDraw
CheckDraw_fin:
ret
PerformWriteFLP:
call ParseHeadTrackSectorDriveSize
cmp byte [badHead], 1
je PerformWriteFLP_fin
cmp byte [badTrack], 1
je PerformWriteFLP_fin
cmp byte [badSector], 1
je PerformWriteFLP_fin
cmp byte [badDrive], 1
je PerformWriteFLP_fin
cmp byte [badSize], 1
je PerformWriteFLP_fin
call GetText
call ConvertSizeToSectors
; append # if the inserted string is shorter than specified size
mov AX, word [textDataLen]
sub AX, word [size]
jns size_not_greather
mov AX, word [size]
mov SI, textData
add SI, word [textDataLen]
loop_append_dellar:
mov byte [SI], "#"
inc word [textDataLen]
inc SI
cmp word [textDataLen], AX
jne loop_append_dellar
size_not_greather:
; write on floppy disk - takes data from ES:BX
mov AH, 03H
mov AL, byte [sectorCount]
mov CH, byte [track]
mov CL, byte [sector]
mov DL, byte [drive]
mov DH, byte [head]
mov BX, textData
int 13h
PerformWriteFLP_fin:
mov byte [known], 1
ret
PerformReadFLP:
call ParseHeadTrackSectorDriveSize
cmp byte [badHead], 1
je PerformReadFLP_fin
cmp byte [badTrack], 1
je PerformReadFLP_fin
cmp byte [badSector], 1
je PerformReadFLP_fin
cmp byte [badDrive], 1
je PerformReadFLP_fin
cmp byte [badSize], 1
je PerformReadFLP_fin
call ConvertSizeToSectors
; read from floppy disk - result in ES:BX
mov AH, 02H
mov AL, byte [sectorCount]
mov CH, byte [track]
mov CL, byte [sector]
mov DL, byte [drive]
mov DH, byte [head]
mov BX, textData
; mov ES, BX
; xor BX, BX
int 13h
; print text data on th sreen
mov CX, 0
mov AH, 0eh
mov SI, BX
mov BX, 0
print_data_loop:
mov AL, [SI]
mov BL, 0
int 10h
inc SI
inc CX
cmp CX, word [size]
jne print_data_loop
PerformReadFLP_fin:
mov byte [known], 1
ret
PerformAbout:
mov byte [known], 1
; read from floppy disk - result in ES:BX
mov AH, 02H
mov AL, 1
mov CH, 51 ; track
mov CL, 4 ; sector
mov DL, 0
mov DH, 0
mov BX, textData
; mov ES, BX
; xor BX, BX
int 13h
; print text data on th sreen
mov CX, 0
mov AH, 0eh
mov SI, BX
mov BX, 0
about_loop:
mov AL, [SI]
mov BL, 0
int 10h
inc SI
inc CX
cmp CX, 100
jne about_loop
ret
PerformAscii:
mov byte [known], 1
mov byte [s], 48
mov byte [z], 48
mov byte [u], 48
mov CL, 0
mov AH, 0eh
mov BL, 0
jmp skip_updateSZU
updateSZU:
inc byte [u]
cmp byte [u], 58
je incZ
jmp updateSZU_fin
incZ:
mov byte [u], 48
inc byte [z]
cmp byte [z], 58
je incS
jmp updateSZU_fin
incS:
mov byte [z], 48
inc byte [s]
updateSZU_fin:
ret
skip_updateSZU:
loop_chars:
mov AH, 0eh
mov AL, byte [s]
int 10H
mov AL, byte [z]
int 10H
mov AL, byte [u]
int 10H
mov AL, "="
int 10H
; put char
cmp CL, 10
je spaceInstead
mov AL, CL
int 10H
jmp notSpaceInstead
spaceInstead:
mov AL, " "
int 10H
notSpaceInstead:
call updateSZU
inc CL
mov AH, 0
mov AL, CL
mov byte [chrOnLine], 13
div byte [chrOnLine]
cmp AH, 0
je twoSpaces
jmp skipTwoSpaces
twoSpaces:
call newline
skipTwoSpaces:
mov AH, 0eh
mov AL, " "
int 10H
cmp CL, 255
jne loop_chars
ret
PerformHelp:
mov byte [known], 1
call GetCursorPosition
mov AX, 1301h
mov BH, 00h
mov BL, 03h
mov CX, abTextLen
mov BP, abText
int 10h
ret
PerformClear:
mov byte [known], 1
mov byte [wasClear], 1
; mov AH, 06H
; mov AL, 00H
; mov BH, 07H
; mov CH, 24
; mov CL, 79
; mov DX, 0000H
; int 10H
;set text video mode
mov AH, 00h
mov AL, 02h
int 10h
ret
PerformReboot:
mov byte [known], 1
int 19h
ret
PerformBeep:
mov byte [known], 1
mov AL, 0b6H
out 43H, AL
mov AL, 0d0H
out 42H, AL
in AL, 61H
or AL, 03H
out 61H, AL
mov CX, 0007H
mov DX, 0a120H
mov AH, 86H
int 15H
in AL, 61H
and AL, 0fcH
out 61H, AL
; mov AH, 0eH
; mov AL, 07H
; int 10h
ret
PerformChrono:
mov byte [known], 1
mov byte [drawStopped], 0
mov byte [number], 0
loop_chrono:
call PerformClear
call PrintNumber
call Delay
inc byte [number]
mov AH, 01h
int 16h
cmp AH, 1cH
jne SkipSetStopDraw
mov byte [drawStopped], 1
SkipSetStopDraw:
cmp byte [drawStopped], 1
jne loop_chrono
call PerformClear
ret
PerformFib:
mov byte [known], 1
mov BX, cmd
performFib_find_space:
inc BX
cmp byte [BX], " "
jne performFib_find_space
mov byte [ten], 10
; parse fibnmb
mov AX, 0
mov byte [fibnmb], 0
inc BX
loop_fibnmb:
mul byte [ten]
mov DL, byte [BX]
sub DL, 48
add AL, DL
inc BX
cmp byte [BX], ":"
jne loop_fibnmb
mov byte [fibnmb], AL
cmp byte [fibnmb], 1
jne PerformFib_skip1
mov AL, 49
call printchar
jmp PerformFib_fin
PerformFib_skip1:
cmp byte [fibnmb], 2
jne PerformFib_skip2
mov AL, "1"
call printchar
mov AL, " "
call printchar
mov AL, "1"
call printchar
jmp PerformFib_fin
PerformFib_skip2:
mov AL, "1"
call printchar
mov AL, " "
call printchar
mov AL, "1"
call printchar
mov AL, " "
call printchar
mov byte [fiba], 1
mov byte [fibb], 1
mov CL, 2
loop_fib:
inc CL
mov DH, byte [fiba]
add DH, byte [fibb]
mov byte [fibc], DH
mov byte [number], DH
call PrintNumber
mov AL, " "
call printchar
mov DH, byte [fibb]
mov byte [fiba], DH
mov DH, byte [fibc]
mov byte [fibb], DH
cmp CL, byte [fibnmb]
jne loop_fib
PerformFib_fin:
ret
PerformDraw:
mov byte [known], 1
cli ; Disable interrupts to circumvent bug on early 8088 CPUs
mov ss,bx ; Top of the stack @ 0x80000.
mov sp,ax ; Set SP=0 so the bottom of stack will be just below 0x90000
sti ; Re-enable interrupts
cld
jmp 5000h:0000h
ret
GetText:
jmp skip_GetText_Checkkey
GetText_Checkkey:
;check if Enter has been pressed
cmp AH, 1cH
je GetText_Checkkey_Enter
;check if bksp has been pressed
cmp AH, 0eH
je GetText_Checkkey_GetText_bksp
;check if up key has been pressed
cmp AH, 48H
je GetText_Checkkey_fin
;check if right key has been pressed
cmp AH, 4dH
je GetText_Checkkey_fin
;check if down key has been pressed
cmp AH, 50H
je GetText_Checkkey_fin
;check if left key has been pressed
cmp AH, 4bH
je GetText_Checkkey_fin
call printchar
call AppendToText
jmp GetText_Checkkey_fin
GetText_Checkkey_GetText_bksp:
call GetText_bksp
jmp GetText_Checkkey_fin
GetText_Checkkey_Enter:
call GetText_Enter
jmp GetText_Checkkey_fin
GetText_Checkkey_fin:
ret
skip_GetText_Checkkey:
jmp skip_AppendToText
AppendToText:
;put char in memory using textDataLen position
mov BX, textData
mov DX, word [textDataLen]
mov DH, 00H
add BX, DX
mov [BX], AL
;increment textDataLen and CL
inc word [textDataLen]
ret
skip_AppendToText:
jmp skip_GetText_Enter
GetText_Enter:
mov byte [gotText], 1
ret
skip_GetText_Enter:
jmp skip_GetText_bksp
GetText_bksp:
;decrement textDataLen
cmp word [textDataLen], 0
je GetText_bksp_fin
dec word [textDataLen]
call cursorback ;moves the cursot back
call putspace ;put a space where the cursor is located
GetText_bksp_fin:
ret
skip_GetText_bksp:
mov byte [gotText], 0
mov word [textDataLen], 0
get_text_loop:
call readkey
call GetText_Checkkey
cmp byte [gotText], 0
je get_text_loop
ret
ParseHeadTrackSectorDriveSize:
mov BX, cmd
find_space:
inc BX
cmp byte [BX], " "
jne find_space
mov byte [ten], 10
mov byte [badHead], 0
mov byte [badTrack], 0
mov byte [badSector], 0
mov byte [badDrive], 0
mov byte [badSize], 0
; parse head number
mov AX, 0
mov byte [head], 0
inc BX
loop_head:
mul byte [ten]
mov DL, byte [BX]
sub DL, 48
add AL, DL
inc BX
cmp byte [BX], ","
jne loop_head
mov byte [head], AL
cmp byte [head], 1
jg badHeadJmp
; parse track number
mov AX, 0
mov byte [track], 0
inc BX
loop_track:
mul byte [ten]
mov DL, byte [BX]
sub DL, 48
add AL, DL