-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.asm
More file actions
1064 lines (828 loc) · 20.1 KB
/
kernel.asm
File metadata and controls
1064 lines (828 loc) · 20.1 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
org 0x7e00
jmp 0x0000:START
%macro random 1
mov word [modulo], %1
call RAND
%endmacro
modulo dw 0
RAND:
mov ah, 00h ; interrupt to get system time
int 1ah ; CX:DX has number of clock ticks since midnight (00:00)
mov ax, dx
xor dx, dx
mov cx, word [modulo]
div cx
mov cx, 1
ret
START:
xor ax, ax
mov ds, ax
mov es, ax
mov bl, 15
mov al, 10h
int 10h
call RESET_CURSOR
call PRINT_LOGO
mov ah, 2
mov bh, 0
mov dh, 14
mov dl, 17
int 10h
mov si, login
call PRINT
mov di, username ; READ occurs in di
call READ
call CLEAR_SCREEN
startBegin:
mov si, startHeader1
call PRINT_COLOR
mov si, startHeader2
call PRINT_COLOR
mov si, startHeader3
call PRINT
mov si, startHeader4
call PRINT_COLOR
mov si, startHeader5
call PRINT_COLOR
.while:
mov si, username
call PRINT_COLOR
mov si, aux
call PRINT
mov di, command
call READ
call ANALYZE_COMMAND
jmp .while
jmp end
STRCMP:
lodsb ; getting current char pointed by SI and putting it on AL
;;;;;;
; mov ah, 0xe
; mov bh, 0
; int 10h ;;debug
cmp byte[di], al ;comparing loaded byte from [ES:SI] with [ES:DI]
jne .false ; if different, then strings aren't equal
cmp al, 0 ; string end? then analyze if it is also the end of the other one
je .si_end
cmp byte[di], 0 ; same as above
je .di_end
inc di
jmp STRCMP
.si_end:
cmp byte[di], 0
je .true
jmp .false
.di_end:
cmp al, 0
je .true
jmp .false
.true:
mov cx, 1
ret
.false:
mov cx, 0
ret
RESET_CURSOR:
mov ah, 2
mov bh, 0
mov dh, 0
mov dl, 0
int 10h
ret
GENERATE_RANDOM_NUMBER:
random 1000
mov dh, 0
mov ax, dx
ret
ANALYZE_COMMAND:
.help:
mov di, command
mov si, help
xor cx, cx
call STRCMP
cmp cl, 0
je .clear ; if its not help, try exit
mov si, menu1
call PRINT
mov si, menu2
call PRINT
mov si, getHelpChoice
call PRINT
mov di, temp
call READ
mov di, temp
mov si, choice1 ; comparing with '1'
call STRCMP
cmp cl, 1
je .help.choice1
mov di, temp
mov si, choice2 ; comparing with '2'
call STRCMP
cmp cl, 1
je .help.choice2
jmp .error
.help.choice1:
mov si, availableCommands1
call PRINT
mov si, availableCommands2
call PRINT
mov si, availableCommands3
call PRINT
mov si, availableCommands4
call PRINT
mov si, availableCommands5
call PRINT
mov si, availableCommands6
call PRINT
mov si, availableCommands7
call PRINT
mov si, availableCommands8
call PRINT
jmp .done
.help.choice2:
mov si, creditsHelp
call PRINT
jmp .done
.clear:
mov di, command
mov si, clearCommand
call STRCMP
cmp cl, 0
je .game ; if command is not clear, try game
call CLEAR_SCREEN
jmp .done
.game:
mov di, command
mov si, gameCommand
call STRCMP
cmp cl, 0
je .exit ; if command is not game, try exit
mov si, gamePresentation1
call PRINT
mov si, gamePresentation2
call PRINT
mov si, gamePresentation3
call PRINT
mov si, gamePresentation4
call PRINT
mov si, gamePresentation5
call PRINT
mov si, gamePresentation6
call PRINT
mov si, gamePresentation7
call PRINT
mov si, gamePresentation8
call PRINT
mov si, gamePresentation9
call PRINT
mov si, gamePresentation10
call PRINT
mov si, gamePresentation11
call PRINT
mov si, gamePresentation12
call PRINT
mov si, gamePresentation13
call PRINT
mov di, temp
call READ
mov di, temp
mov si, y
call STRCMP
cmp cl, 0
je .done
call GENERATE_RANDOM_NUMBER
push ax
xor dx, dx
.game.while:
cmp dl, 10
je .game.tooManyTries
inc dl
mov si, askInput
call PRINT
mov di, guess
call READ
mov di, guessNumber
mov si, guess
call transform
mov di, guessNumber
mov ah, byte[di]
inc di
mov al, byte[di]
dec di
mov bx, ax
pop ax
cmp ax, bx
jg .game.high
cmp ax, bx
jl .game.low
mov si, correctAnswer1
call PRINT_COLOR
mov si, correctAnswer2
call PRINT
mov si, correctAnswer3
call PRINT_COLOR
jmp .game.done
.game.high:
push ax
mov si, numberIsHigher
call PRINT_COLOR
jmp .game.while
.game.low:
push ax
mov si, numberIsLower
call PRINT_COLOR
jmp .game.while
.game.tooManyTries:
mov si, wrongAnswer1
call PRINT_COLOR
mov si, wrongAnswer2
call PRINT
mov si, wrongAnswer3
call PRINT_COLOR
pop ax
jmp .game.done
.game.done:
jmp .done
.exit:
mov di, command
mov si, exit
xor cx, cx
call STRCMP
cmp cl, 0
je .credits ; if its not exit, try credits
mov si, ctz
call PRINT
mov di, temp
call READ
mov di, temp
mov si, y
call STRCMP
cmp cl, 1
je end
jmp .done
.credits:
mov di, command
mov si, creditsCommand
xor cx, cx
call STRCMP
cmp cl, 0
je .add ; if not credits, try add
mov si, credits0
call PRINT
mov si, credits1
call PRINT
mov si, credits2
call PRINT
mov si, credits3
call PRINT
mov si, credits4
call PRINT
mov si, credits5
call PRINT
jmp .done
.add:
mov di, command
mov si, addCommand
xor cx, cx
call STRCMP
cmp cl, 0
je .sub ; if its not add, try sub
mov si, escolha
call PRINT
call READ_NUMBER
add ax, bx
mov di, valuef
mov byte[di], ah
inc di
mov byte[di], al
dec di
mov si, valuef
mov di, numf
call transform2
mov si, numf
call inverte_str
mov si, numf
call PRINT
; continue addition
jmp .done
.sub:
mov di, command
mov si, subCommand
xor cx, cx
call STRCMP
cmp cl, 0
je .mult ; if its not sub, try mult
mov si, escolhaSub
call PRINT
call READ_NUMBER
mov cx, 0
cmp ax, bx
jge .positivo
mov cx, 1
xor ax, bx
xor bx, ax ; swap
xor ax, bx
.positivo:
sub ax, bx
cmp cx, 0
je .certo
mov si, menos
push ax
call PRINT
pop ax
.certo:
mov di, valuef
mov byte[di], ah
inc di
mov byte[di], al
dec di
mov si, valuef
mov di, numf
call transform2
mov si, numf
call inverte_str
mov si, numf
call PRINT
; work sub
jmp .done
.mult:
mov di, command
mov si, multCommand
xor cx, cx
call STRCMP
cmp cl, 0
je .div ; if its not mult, try div
mov si, escolhaMult
call PRINT
call READ_NUMBER
mul bx
mov di, valuef
mov byte[di], ah
inc di
mov byte[di], al
dec di
mov si, valuef
mov di, numf
call transform2
mov si, numf
call inverte_str
mov si, numf
call PRINT
; work mult
jmp .done
.div:
mov di, command
mov si, divCommand
xor cx, cx
call STRCMP
cmp cl, 0
je .error ; if its not sub, then its error
mov si, escolhaDiv
call PRINT
call READ_NUMBER
mov si, value2
mov di, zeroString
call STRCMP
cmp cl, 0
je .div.byZero
mov si, quotientResult
call PRINT
mov si, value1
mov ah, byte[si]
inc si
mov al, byte[si]
mov si, value2
mov bh, byte[si]
inc si
mov bl, byte[si]
dec si
xor dx, dx
cmp bx, 0
je .div.byZero
div bx
mov di, valuef
mov byte[di], ah
inc di
mov byte[di], al
dec di
mov si, valuef
mov di, numf
call transform2
mov si, numf
call inverte_str
mov si, numf
call PRINT
mov si, remainderResult
call PRINT
mov si, value1
mov ah, byte[si]
inc si
mov al, byte[si]
mov si, value2
mov bh, byte[si]
inc si
mov bl, byte[si]
dec si
xor dx, dx
div bx
mov ax, dx
mov di, valuef
mov byte[di], ah
inc di
mov byte[di], al
dec di
mov si, valuef
mov di, numf
call transform2
mov si, numf
call inverte_str
mov si, numf
call PRINT
; work div
jmp .done
.div.byZero:
mov si, divByZero
call PRINT
jmp .done
.error:
mov di, command
mov si, unknownCommand
call PRINT
jmp .done
.done:
ret
READ_NUMBER:
mov di, num1
call READ
mov di, value1
mov si, num1
call transform
mov di, num2
call READ
mov di, value2
mov si, num2
call transform
mov di, value1
mov ah, byte[di]
inc di
mov al, byte[di]
dec di
mov di, value2
mov bh, byte[di]
inc di
mov bl, byte[di]
dec di
ret
transform2:
mov bh, byte[si]
inc si
mov bl, byte[si]
dec si
cmp bx, 0
je .end2
mov ch, 0
mov cl, 10
.for1:
cmp bx, 0
je .end
mov ax, bx
mov dx, 0
div cx
mov bx, ax
mov ax, dx
add al, '0'
stosb
jmp .for1
.end:
mov al, 0
stosb
ret
.end2:
mov al, '0'
stosb
mov al, 0
stosb
ret
transform:
xor ax, ax
xor bx, bx
xor cl, cl
.for1:
push si
mov ch, 0
add si, cx
mov ch, byte[si]
cmp ch, 0
je .end
sub ch, '0'
mov ax, 0
mov al, 10
mul bl
mov bx, ax
add bl, ch
inc cl
pop si
jmp .for1
.end:
mov byte[di], bh
inc di
mov byte[di], bl
dec di
pop si
ret
inverte_str:
xor cx, cx
.for1:
mov dx, si
add si, cx
cmp byte[si], 0
je .endfor1
movzx bx, byte[si]
push bx
mov si, dx
inc cx
jmp .for1
.endfor1:
mov si, dx
xor ax, ax
.for2:
cmp ax, cx
je .end
mov dx, si
add si, ax
pop bx
mov byte[si], bl
inc ax
mov si, dx
jmp .for2
.end:
mov si, dx
add si, cx
mov byte[si], 10
mov si, dx
add si, cx
add si, 1
mov byte[si], 13
add si, 1
mov byte[si], 0
ret
READ:
xor cx, cx
mov bl, 15
.for1:
mov ah, 0
int 16h
cmp al, 0x0d ;string end
je .end
cmp al, 0x08
je .backspace
mov ah, 0x0e
int 10h
inc cx
stosb
jmp .for1
.backspace:
cmp cl, 0 ; goes back if theres nothing to erase
je .for1
; removing last character
dec cx
dec di
mov ah, 0x0e
mov al, 0x08 ;;printing backspace
int 10h
mov al, 0x20 ;;printing space
int 10h
mov al, 0x08 ;printing backspace again
int 10h
jmp .for1
.end:
mov al, 0
stosb
mov ah, 0x0e
mov al, 10
int 10h
mov al, 13
int 10h
ret
PRINT_COLOR:
lodsb
cmp al, 0
je .done
mov ah, 0xe
mov bl, 2
int 10h
jmp PRINT_COLOR
.done:
ret
PRINT:
lodsb
cmp al, 0
je .done
mov ah, 0xe
mov bl, 15
int 10h
jmp PRINT
.done:
ret
CLEAR_SCREEN:
mov dx, 4500
xor cx, cx
.while:
mov ah, 0x0e
mov al, 0x20 ;print space
int 10h
dec dx
cmp dx, 0
je .end
jmp .while
.end:
mov ah, 2
mov bh, 0
mov dh, 0
mov dl, 0
int 10h
jmp startBegin
PRINT_LOGO:
mov ah, 2
mov bh, 0
mov dh, 2
mov dl, 7
int 10h
mov si, line1
call PRINT_COLOR
mov ah, 2
mov bh, 0
mov dh, 3
mov dl, 7
int 10h
mov si, line2
call PRINT_COLOR
mov ah, 2
mov bh, 0
mov dh, 4
mov dl, 7
int 10h
mov si, line3
call PRINT_COLOR
mov ah, 2
mov bh, 0
mov dh, 5
mov dl, 7
int 10h
mov si, line4
call PRINT_COLOR
mov ah, 2
mov bh, 0
mov dh, 6
mov dl, 7
int 10h
mov si, line5
call PRINT_COLOR
mov ah, 2
mov bh, 0
mov dh, 7
mov dl, 7
int 10h
mov si, line6
call PRINT_COLOR
mov ah, 2
mov bh, 0
mov dh, 8
mov dl, 7
int 10h
mov si, line7
call PRINT_COLOR
mov ah, 2
mov bh, 0
mov dh, 9
mov dl, 7
int 10h
mov si, line8
call PRINT_COLOR
mov ah, 2
mov bh, 0
mov dh, 10
mov dl, 7
int 10h
mov si, line9
call PRINT_COLOR
mov ah, 2
mov bh, 0
mov dh, 11
mov dl, 7
int 10h
mov si, line10
call PRINT_COLOR
mov ah, 2
mov bh, 0
mov dh, 12
mov dl, 7
int 10h
mov si, line11
call PRINT_COLOR
mov ah, 2
mov bh, 0
mov dh, 13
mov dl, 7
int 10h
ret
end:
jmp $
var:
menos db '-', 0
login db 'Type your login: ', 0
aux db '@cin:~$ ', 0
line1 db ' .----------------. .----------------. .----------------. ', 10, 13, 0
line2 db '| .--------------. | | .--------------. | | .--------------. |', 10, 13, 0
line3 db '| | _____ | | | | _______ | | | | ____ ____ | |', 10, 13, 0
line4 db '| | |_ _| | | | | / ____ | | | | ||_ \ / _|| |', 10, 13, 0
line5 db '| | | | | | | | / / \_| | | | | | \/ | | |', 10, 13, 0
line6 db '| | | | _ | | | | | | | | | | | |\ /| | | |', 10, 13, 0
line7 db '| | _| |__/ | | | | | \ \_____/\ | | | | _| |_\/_| |_ | |', 10, 13, 0
line8 db '| | |________| | | | | \_______/ | | | ||_____||_____|| |', 10, 13, 0
line9 db '| | | | | | | | | | | |', 10, 13, 0
line10 db '| |--------------| | | |--------------| | | |--------------| |', 10, 13, 0
line11 db ' ------------------ ------------------ ------------------ ', 10, 13, 0
username: times 20 db 0
command: times 20 db 0
num1: times 10 db 0
num2: times 10 db 0
numf: times 10 db 0
value1: times 4 db 0
value2: times 4 db 0
valuef: times 4 db 0
temp: times 10 db 0
guess: times 10 db 0
generated: times 6 db 0
answer: times 6 db 0
guessNumber: times 20 db 0
startHeader1 db 'LCM Terminal - Version 16.23.49.1', 10, 13, 0
startHeader2 db 'Type ', 0
startHeader3 db '"help" ', 0
startHeader4 db 'for more information.', 10, 13, 0
startHeader5 db '---------------------------------', 10, 13, 0
askInput db 'Enter your guess: (positive integer): ', 0
numberIsLower db '---- The answer is lower than your input ----', 10, 13, 0