-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel-4.1.asm
698 lines (573 loc) · 13.4 KB
/
kernel-4.1.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
;*****************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 _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_info ;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
; display hardware info
_cmd_info:
mov si, strCmd0
mov di, cmdInfo
mov cx, 5
repe cmpsb
jne _cmd_exit ;next command
call _display_endl
call _display_hardware_info ;display Information
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_prompt:
mov si, strPrompt
mov al, 0x01
int 0x21
ret
_display_hardware_info: ; Procedure for printing Hardware info
push ax
push bx
push cx
push dx
push es
push si
call _display_endl
mov si, strInfo ; Prints the command description
mov al, 0x01
int 0x21
call _display_endl
call _display_endl
mov si, strmemory ; Prints base memory string
mov al, 0x01
int 0x21
; Reading Base Memory -----------------------------------------------
push ax
push dx
int 0x12 ; call interrupt 12 to get base mem size
mov dx,ax
mov [basemem] , ax
call _print_dec ; display the number in decimal
mov al, 0x6b
mov ah, 0x0E ; BIOS teletype acts on 'K'
mov bh, 0x00
mov bl, 0x07
int 0x10
pop dx
pop ax
; Reading extended Memory
call _display_endl
mov si, strsmallextended
mov al, 0x01
int 0x21
xor cx, cx ; Clear CX
xor dx, dx ; clear DX
mov ax, 0xE801
int 0x15 ; call interrupt 15h
mov dx, ax ; save memory value in DX as the procedure argument
mov [extmem1], ax
call _print_dec ; print the decimal value in DX
mov al, 0x6b
mov ah, 0x0E ; BIOS teletype acts on 'K'
mov bh, 0x00
mov bl, 0x07
int 0x10
xor cx, cx ; clear CX
xor dx, dx ; clear DX
mov ax, 0xE801
int 0x15 ; call interrupt 15h
mov ax, dx ; save memory value in AX for division
xor dx, dx
mov si , 16
div si ; divide AX value to get the number of MB
mov dx, ax
mov [extmem2], ax
push dx ; save dx value
call _display_endl
mov si, strbigextended
mov al, 0x01
int 0x21
pop dx ; retrieve DX for printing
call _print_dec
mov al, 0x4D
mov ah, 0x0E ; BIOS teletype acts on 'M'
mov bh, 0x00
mov bl, 0x07
int 0x10
call _display_endl
mov si, strtotalmemory
mov al, 0x01
int 0x21
; total memory = basemem + extmem1 + extmem2
mov ax, [basemem]
add ax, [extmem1] ; ax = ax + extmem1
shr ax, 10
add ax, [extmem2] ; ax = ax + extmem2
mov dx, ax
call _print_dec
mov al, 0x4D
mov ah, 0x0E ; BIOS teletype acts on 'M'
mov bh, 0x00
mov bl, 0x07
int 0x10
;CPU Information --------------------------------------------------------------------------
call _display_endl
mov si, strCPUVendor
mov al, 0x01
int 0x21
mov eax, 0x00000000 ; set eax register to get the vendor
cpuid
mov eax, ebx ; prepare for string saving
mov ebx, edx
mov edx, 0x00
mov si, strVendorID
call _save_string
mov si, strVendorID ;print string
mov al, 0x01
int 0x21
call _display_endl
mov si, strCPUdescription
mov al, 0x01
int 0x21
mov eax, 0x80000000 ; First check if CPU support this
cpuid
cmp eax, 0x80000004
jb _cpu_not_supported ; if not supported jump to function end
mov eax, 0x80000002 ; get first part of the brand
mov si, strBrand
cpuid
call _save_string
add si, 16
mov eax, 0x80000003 ; get second part of the brand
cpuid
call _save_string
add si, 16
mov eax, 0x80000004 ; get third part of the brand
cpuid
call _save_string
mov si, strBrand ; print the saved Brand string
mov al, 0x01
int 0x21
jmp _hard_info
_cpu_not_supported:
mov si, strNotSupported
mov al, 0x01
int 0x21
;End of processor info
; Number of Harddrives -------------------------------------------------------------
_hard_info:
call _display_endl
mov si, strhdnumber
mov al, 0x01
int 0x21
mov ax,0040h ; look at 0040:0075 for a number
mov es,ax ;
mov dl,[es:0075h] ; move the number into DL register
add dl,30h ; add 48 to get ASCII value
mov al, dl
mov ah, 0x0E ; BIOS teletype acts on character
mov bh, 0x00
mov bl, 0x07
int 0x10
_serial_ports:
call _display_endl
mov si, strserialportnumber
mov al, 0x01
int 0x21
mov ax, [es:0x10]
shr ax, 9
and ax, 0x0007
add al, 30h
mov ah, 0x0E ; BIOS teletype acts on character
mov bh, 0x00
mov bl, 0x07
int 0x10
; Reading base I/O addresses
;Base I/O address for serial port 1 (communications port 1 - COM 1)
mov ax, [es:0000h] ; Read address for serial port 1
cmp ax, 0
je _end
call _display_endl
mov si, strserialport1
mov al, 0x01
int 0x21
mov dx, ax
call _print_dec
_end:
;Base I/O address for serial port 1 (communications port 1 - COM 1)
call _display_endl
pop si
pop es
pop dx
pop cx
pop bx
pop ax
ret
_print_dec:
push ax ; save AX
push cx ; save CX
push si ; save SI
mov ax,dx ; copy number to AX
mov si,10 ; SI is used as the divisor
xor cx,cx ; clear 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 ; clear AX
jne _non_zero ; if not go to _non_zero
_prepare_digits:
pop dx ; get the digit from DX
add dl,0x30 ; add 30 to get the ASCII value
call _print_char ; print char
loop _prepare_digits ; loop till cx == 0
pop si ; restore SI
pop cx ; restore CX
pop ax ; restore AX
ret
_print_char:
push ax ; save AX
mov al, dl
mov ah, 0x0E ; BIOS teletype acts on printing char
mov bh, 0x00
mov bl, 0x07
int 0x10
pop ax ; restore AX
ret
_save_string:
mov dword [si], eax
mov dword [si+4], ebx
mov dword [si+8], ecx
mov dword [si+12], edx
ret
[SEGMENT .data]
strWelcomeMsg db "Welcome to JOSH Ver 0.04", 0x00
strPrompt db "JOSH>>", 0x00
cmdMaxLen db 255 ;maximum length of commands
strOsName db "JOSH", 0x00 ;OS details
strMajorVer db "0", 0x00
strMinorVer db ".04", 0x00
cmdVer db "ver", 0x00 ; internal commands
cmdExit db "exit", 0x00
cmdInfo db "info", 0x00 ; Shows hardware information
txtVersion db "version", 0x00 ;messages and other strings
msgUnknownCmd db "Unknown command or bad file name!", 0x00
strInfo db "||---------------------- Hardware Information ----------------------|| ", 0x00
strmemory db "Base Memory size: ", 0x00
strsmallextended db "Extended memory between(1M - 16M): ", 0x00
strbigextended db "Extended memory above 16M: ", 0x00
strCPUVendor db "CPU Vendor : ", 0x00
strCPUdescription db "CPU description: ", 0x00
strNotSupported db "Not supported.", 0x00
strhdnumber db "Number of hard drives: ",0x00
strserialportnumber db "Number of serial ports: ", 0x00
strserialport1 db "Base I/O address for serial port 1 (communications port 1 - COM 1): ", 0x00
strtotalmemory db "Total memory: ", 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
strVendorID resb 16
strBrand resb 48
basemem resb 2
extmem1 resb 2
extmem2 resb 2
;********************end of the kernel code********************