-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkernel-3.0.asm
More file actions
741 lines (601 loc) · 12.5 KB
/
kernel-3.0.asm
File metadata and controls
741 lines (601 loc) · 12.5 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
;R.M.S. Rathnayake
;090436X
;*****************start of the kernel code***************
[org 0x000]
[bits 16]
[SEGMENT .text]
;START #####################################################
mov ax, 0x0100 ;location where kernel is loaded
mov ds, ax
mov es, ax
cli
mov ss, ax ;stack segment
mov sp, 0xFFFF ;stack pointer at 64k limit
sti
push dx
push es
xor ax, ax
mov es, ax
cli
mov word [es:0x21*4], _int0x21 ; setup interrupt service
mov [es:0x21*4+2], cs
sti
pop es
pop dx
mov si, strWelcomeMsg ; load message
mov al, 0x01 ; request sub-service 0x01
int 0x21
call _display_endl
call _display_endl
mov si, strInfo ; load message
mov al, 0x01 ; request sub-service 0x01
int 0x21
call _display_endl
call _shell ; call the shell
int 0x19 ; reboot
;END #######################################################
_int0x21:
_int0x21_ser0x01: ;service 0x01
cmp al, 0x01 ;see if service 0x01 wanted
jne _int0x21_end ;goto next check (now it is end)
_int0x21_ser0x01_start:
lodsb ; load next character
or al, al ; test for NUL character
jz _int0x21_ser0x01_end
mov ah, 0x0E ; BIOS teletype
mov bh, 0x00 ; display page 0
mov bl, 0x07 ; text attribute
int 0x10 ; invoke BIOS
jmp _int0x21_ser0x01_start
_int0x21_ser0x01_end:
jmp _int0x21_end
_int0x21_end:
iret
_shell:
_shell_begin:
;move to next line
call _display_endl
;display prompt
call _display_prompt
;get user command
call _get_command
;split command into components
call _split_cmd
;check command & perform action
; empty command
_cmd_none:
mov si, strCmd0
cmp BYTE [si], 0x00
jne _cmd_ver ;next command
jmp _cmd_done
; display version
_cmd_ver:
mov si, strCmd0
mov di, cmdVer
mov cx, 4
repe cmpsb
jne _cmd_list ;next command
call _display_endl
mov si, strOsName ;display version
mov al, 0x01
int 0x21
call _display_space
mov si, txtVersion ;display version
mov al, 0x01
int 0x21
call _display_space
mov si, strMajorVer
mov al, 0x01
int 0x21
mov si, strMinorVer
mov al, 0x01
int 0x21
jmp _cmd_done
_cmd_list:
mov si, strCmd0
mov di, cmdInfo
mov cx, 7
repe cmpsb
jne _cmd_HardInfo ;next command
call _info
jmp _cmd_done
_cmd_HardInfo:
mov si, strCmd0
mov di, cmdHardInfo
mov cx, 4
repe cmpsb
jne _cmd_exit ;next command
call _display_endl
call _cpuid
call _bios_date
call _mem_detect
call _diskette
call _hard_drive
call _display_serial_ports
call _display_endl
jmp _cmd_done
; exit shell
_cmd_exit:
mov si, strCmd0
mov di, cmdExit
mov cx, 5
repe cmpsb
jne _cmd_unknown ;next command
je _shell_end ;exit from shell
_cmd_unknown:
call _display_endl
mov si, msgUnknownCmd ;unknown command
mov al, 0x01
int 0x21
_cmd_done:
;call _display_endl
jmp _shell_begin
_shell_end:
ret
_get_command:
;initiate count
mov BYTE [cmdChrCnt], 0x00
mov di, strUserCmd
_get_cmd_start:
mov ah, 0x10 ;get character
int 0x16
cmp al, 0x00 ;check if extended key
je _extended_key
cmp al, 0xE0 ;check if new extended key
je _extended_key
cmp al, 0x08 ;check if backspace pressed
je _backspace_key
cmp al, 0x0D ;check if Enter pressed
je _enter_key
mov bh, [cmdMaxLen] ;check if maxlen reached
mov bl, [cmdChrCnt]
cmp bh, bl
je _get_cmd_start
;add char to buffer, display it and start again
mov [di], al ;add char to buffer
inc di ;increment buffer pointer
inc BYTE [cmdChrCnt] ;inc count
mov ah, 0x0E ;display character
mov bl, 0x07
int 0x10
jmp _get_cmd_start
_extended_key: ;extended key - do nothing now
jmp _get_cmd_start
_backspace_key:
mov bh, 0x00 ;check if count = 0
mov bl, [cmdChrCnt]
cmp bh, bl
je _get_cmd_start ;yes, do nothing
dec BYTE [cmdChrCnt] ;dec count
dec di
;check if beginning of line
mov ah, 0x03 ;read cursor position
mov bh, 0x00
int 0x10
cmp dl, 0x00
jne _move_back
dec dh
mov dl, 79
mov ah, 0x02
int 0x10
mov ah, 0x09 ; display without moving cursor
mov al, ' '
mov bh, 0x00
mov bl, 0x07
mov cx, 1 ; times to display
int 0x10
jmp _get_cmd_start
_move_back:
mov ah, 0x0E ; BIOS teletype acts on backspace!
mov bh, 0x00
mov bl, 0x07
int 0x10
mov ah, 0x09 ; display without moving cursor
mov al, ' '
mov bh, 0x00
mov bl, 0x07
mov cx, 1 ; times to display
int 0x10
jmp _get_cmd_start
_enter_key:
mov BYTE [di], 0x00
ret
_split_cmd:
;adjust si/di
mov si, strUserCmd
;mov di, strCmd0
;move blanks
_split_mb0_start:
cmp BYTE [si], 0x20
je _split_mb0_nb
jmp _split_mb0_end
_split_mb0_nb:
inc si
jmp _split_mb0_start
_split_mb0_end:
mov di, strCmd0
_split_1_start: ;get first string
cmp BYTE [si], 0x20
je _split_1_end
cmp BYTE [si], 0x00
je _split_1_end
mov al, [si]
mov [di], al
inc si
inc di
jmp _split_1_start
_split_1_end:
mov BYTE [di], 0x00
;move blanks
_split_mb1_start:
cmp BYTE [si], 0x20
je _split_mb1_nb
jmp _split_mb1_end
_split_mb1_nb:
inc si
jmp _split_mb1_start
_split_mb1_end:
mov di, strCmd1
_split_2_start: ;get second string
cmp BYTE [si], 0x20
je _split_2_end
cmp BYTE [si], 0x00
je _split_2_end
mov al, [si]
mov [di], al
inc si
inc di
jmp _split_2_start
_split_2_end:
mov BYTE [di], 0x00
;move blanks
_split_mb2_start:
cmp BYTE [si], 0x20
je _split_mb2_nb
jmp _split_mb2_end
_split_mb2_nb:
inc si
jmp _split_mb2_start
_split_mb2_end:
mov di, strCmd2
_split_3_start: ;get third string
cmp BYTE [si], 0x20
je _split_3_end
cmp BYTE [si], 0x00
je _split_3_end
mov al, [si]
mov [di], al
inc si
inc di
jmp _split_3_start
_split_3_end:
mov BYTE [di], 0x00
;move blanks
_split_mb3_start:
cmp BYTE [si], 0x20
je _split_mb3_nb
jmp _split_mb3_end
_split_mb3_nb:
inc si
jmp _split_mb3_start
_split_mb3_end:
mov di, strCmd3
_split_4_start: ;get fourth string
cmp BYTE [si], 0x20
je _split_4_end
cmp BYTE [si], 0x00
je _split_4_end
mov al, [si]
mov [di], al
inc si
inc di
jmp _split_4_start
_split_4_end:
mov BYTE [di], 0x00
;move blanks
_split_mb4_start:
cmp BYTE [si], 0x20
je _split_mb4_nb
jmp _split_mb4_end
_split_mb4_nb:
inc si
jmp _split_mb4_start
_split_mb4_end:
mov di, strCmd4
_split_5_start: ;get last string
cmp BYTE [si], 0x20
je _split_5_end
cmp BYTE [si], 0x00
je _split_5_end
mov al, [si]
mov [di], al
inc si
inc di
jmp _split_5_start
_split_5_end:
mov BYTE [di], 0x00
ret
_display_space:
mov ah, 0x0E ; BIOS teletype
mov al, 0x20
mov bh, 0x00 ; display page 0
mov bl, 0x07 ; text attribute
int 0x10 ; invoke BIOS
ret
_display_endl:
mov ah, 0x0E ; BIOS teletype acts on newline!
mov al, 0x0D
mov bh, 0x00
mov bl, 0x07
int 0x10
mov ah, 0x0E ; BIOS teletype acts on linefeed!
mov al, 0x0A
mov bh, 0x00
mov bl, 0x07
int 0x10
ret
_display_serial_ports:
call _display_endl
mov si, strSerial
mov al, 0x01
int 0x21
xor ax, ax
int 0x11
and ax, 0xe00
shr ax, 9
add ax, 48
mov ah, 0x0e
int 0x10
ret
_diskette:
call _display_endl
mov si, strDiskette
mov al, 0x01
int 0x21
xor ax, ax
int 0x11
and ax, 0x80
shr ax, 6
add ax, 49
mov ah, 0x0e
int 0x10
ret
_info:
call _display_endl
call _display_endl
mov si, strList
mov al, 0x01
int 0x21
call _display_endl
mov si, strVer
mov al, 0x01
int 0x21
call _display_endl
mov si, strHard
mov al, 0x01
int 0x21
call _display_endl
mov si, strReboot
mov al, 0x01
int 0x21
call _display_endl
ret
_hard_drive:
call _display_endl
mov si, strHardDrive
mov al, 0x01
int 0x21
mov ax, 0x0040
push es
mov es,ax
mov al,[es:0x0075] ; read 40:75
add al, 48
pop es
mov ah, 0x0e
int 0x10
ret
_bios_date:
call _display_endl
mov si, strBios
mov al, 0x01
int 0x21
push es
mov ax, 0xf000 ;BIOS release date is saved in F000:FFF5
mov es, ax
mov si, 0xfff5
mov bl,8
_loop:
mov al, [es:si]
mov ah, 0x0e
int 0x10
inc si
dec bl
cmp bl, 0
jne _loop
pop es
ret
_cpuid:
call _display_endl
mov si, strVendor
mov al, 0x01
int 0x21
push eax
push ebx
push ecx
push edx
xor eax, eax
mov eax, 0x00
cpuid
mov [strCPUID], ebx
mov [strCPUID+4], edx
mov [strCPUID+8], ecx
pop edx
pop ecx
pop ebx
pop eax
mov si, strCPUID
call _disp_str
call _display_endl
mov si, strCpuType
mov al, 0x01
int 0x21
mov eax, 0x80000002
mov si, strBrand
cpuid
call _string_store
mov eax, 0x80000003
cpuid
call _string_store
mov eax, 0x80000004
cpuid
call _string_store
add si,16
mov si,0x00
mov si, strBrand
mov al, 0x01
int 0x21
ret
_mem_detect:
call _display_endl
xor ax,ax
xor bx,bx
xor cx,cx
xor dx,dx
mov ax, 0xe801
int 0x15
jc _error
cmp ah, 0x86 ; unsupported function
je _error
cmp ah, 0x80 ; invalid command
je _error
mov si, strMemory
mov al, 0x01
int 0x21
cmp cx, 0x0000
je _cx_zero
jmp _mem_calc
_cx_zero:
mov cx,ax ;some bios return CX=DX=0
mov dx,bx ;copy AX, and BX, to CX/DX
_mem_calc:
shr dx, 4 ;divide dx by 2^4
shr cx, 10 ;divide cx by 2^10 (to convert to MBs)
add cx,dx ;get the total memory
mov dx, cx
call _hex2dec
mov si, strMB
mov al, 0x01
int 0x21
jmp _memory_detected
_error:
mov si, strMemError
mov al, 0x01
int 0x21
_memory_detected:
ret
_disp_str:
lodsb ; load next character
or al, al ; test for NUL character
jz _printed
mov ah, 0x0E ; BIOS teletype
mov bh, 0x00 ; display page 0
mov bl, 0x07 ; text attribute
int 0x10 ; invoke BIOS
jmp _disp_str
_printed:
ret
_display_test:
mov si, strTest
mov al, 0x01
int 0x21
ret
_display_prompt:
mov si, strPrompt
mov al, 0x01
int 0x21
ret
_string_store:
mov dword [si], eax
mov dword [si+4], ebx
mov dword [si+8], ecx
mov dword [si+12], edx
add si, 16
ret
_hex2dec:
;converts the number in dx to decimal and print it
push ax
push bx
push cx
push si
mov ax,dx ; copy number into AX
mov si,10 ; SI will be our divisor
xor cx,cx ; clean up the CX
_non_zero:
xor dx,dx ; clear DX
div si ; divide by 10
push dx ; push number onto the stack
inc cx ; increment CX to do it more times
or ax,ax ; end of the number?
jne _non_zero ; if not repeat
_write_digits:
pop dx ; get the digit off DX
add dl,48 ; add 48 to get ASCII
mov al, dl
mov ah, 0x0e
int 0x10
loop _write_digits
pop si
pop cx
pop bx
pop ax
ret
_print_char:
push ax ; save that AX register
mov al, dl
mov ah, 0x0E ; BIOS teletype acts on newline!
mov bh, 0x00
mov bl, 0x07
int 0x10
pop ax ; restore that AX register
ret
[SEGMENT .data]
strWelcomeMsg db "Welcome to JOSH Ver 0.04 Hacked by Sunimal Rathnayake", 0x00
strPrompt db "JOSH>>", 0x00
strInfo db "Enter cmdlist to view available commands",0x00
strVer db "ver -- version",0x00
strHard db "hdif -- hardware information",0x00
strReboot db "exit -- reboot",0x00
strList db "cmdlist -- available commands",0x00
cmdMaxLen db 255 ;maximum length of commands
strOsName db "JOSH", 0x00 ;OS details
strMajorVer db "0", 0x00
strMinorVer db ".04", 0x00
strTest db "This is just a test.", 0x00
strMemError db "Memory detect error",0x00
strMemory db "Total Memory : ",0x00
strMB db " MB",0x00
strHardDrive db "Hard drives installed : ",0x00
strSerial db "Serial ports available : ",0x00
strDiskette db "Floppy drives available : ",0x00
strVendor db "CPU vendor : ",0x00
strCpuType db "CPU type : ",0x00
strBios db "Bios release date : ",0x00
cmdVer db "ver", 0x00 ; internal commands
cmdExit db "exit", 0x00
cmdTest db "test", 0x00
cmdHardInfo db "hdif", 0x00
cmdDiskette db "floppy", 0x00
cmdInfo db "cmdlist", 0x00
txtVersion db "version", 0x00 ;messages and other strings
msgUnknownCmd db "Unknown command or bad file name!", 0x00
[SEGMENT .bss]
strUserCmd resb 256 ;buffer for user commands
cmdChrCnt resb 1 ;count of characters
strCmd0 resb 256 ;buffers for the command components
strCmd1 resb 256
strCmd2 resb 256
strCmd3 resb 256
strCmd4 resb 256
strBrand resb 256
strCPUID resb 16 ;string variable
;********************end of the kernel code********************