-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.asm
More file actions
198 lines (157 loc) · 5.45 KB
/
kernel.asm
File metadata and controls
198 lines (157 loc) · 5.45 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; nasm -f bin kernel.asm -o kernel.bin
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CPU 8086
ORG 500h
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
command_shell:
;call set_video_mode
call clear_screen
mov ah, 3 ; function: set repeat rate
mov al, 5 ; (AT+)
mov bh, 0 ; delay (0=250ms; AT+)
mov bl, 0 ; repeat rate (4=20cps; 0=30cps)
int 16h ; invoke keyboard driver
mov si, os_title_sz
call write_line
mov si, initial_video_mode_label_sz
call write_string
call get_initial_video_mode
call write_line
call write_blank_line
.write_prompt:
call write_blank_line
mov si, command_prompt_sz
call write_string
.get_keyboard_character:
mov ah, 0 ; function: read character from keyboard
int 16h ; invoke keyboard driver
cmp al, 0Dh ; was `enter` key?
je .process_command ; if `enter` key
.process_character:
xor bx, bx ; clear BX
mov bl, [command_buffer_count] ; copy length of buffer content
mov [command_buffer + bx], al ; append character to buffer content
inc byte [command_buffer_count] ; increment length of buffer content
call write_character
jmp .get_keyboard_character
.process_command:
call write_blank_line
xor cx, cx ; clear CX
mov cl, [command_buffer_count] ; copy length into CL
mov si, command_buffer ; point to command_buffer string
call write_string_sized
mov byte [command_buffer_count], 0 ; reset length to 0
jmp .write_prompt
jmp $ ; effectively halt machine
command_buffer times 256 db 0
command_buffer_count db 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
clear_screen:
mov ah, 6 ; function: initialize window function
mov al, 0 ; blank the window
mov bh, 0111_0000b ; attribute used for blanking
mov ch, 0 ; upper y
mov cl, 0 ; left x
mov dh, 24 ; lower y
mov dl, 79 ; right x
int 0x10 ; invoke display driver
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
get_initial_video_mode:
int 11h ; function: get bios equipment flags
and ax, 11_0000b
cmp ax, 00_0000b
je return_unused_string
cmp ax, 01_0000b
je return_40x25_color_string
cmp ax, 10_0000b
je return_80x25_color_string
cmp ax, 11_0000b
je return_80x25_mono_string
mov si, error_sz
call write_line
jmp $ ; endless loop, effectively halt
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
get_current_video_mode:
mov ah, 0Fh
; TODO
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
return_unused_string:
mov si, unused_sz
ret
return_40x25_color_string:
mov si, color_40x25_sz
ret
return_80x25_color_string:
mov si, color_80x25_sz
ret
return_80x25_mono_string:
mov si, mono_80x25_sz
ret
unused_sz db "unused", 0
color_40x25_sz db "40x25 color", 0
color_80x25_sz db "80x25 color", 0
mono_80x25_sz db "80x25 monochrome", 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
reboot:
jmp 0xFFFF:0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
set_video_mode:
mov ah, 0 ; function: set video mode
mov al, 7 ; 80x25 monochrome text
int 0x10 ; invoke video driver
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
write_number:
add al, 48 ; convert number into ASCII equivalent
; fall through
write_character:
mov ah, 0xE ; function: write character from AL
int 0x10 ; invoke video driver
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
write_blank_line:
mov si, empty_sz
; fall through
write_line:
call write_string
mov si, newline_sz
; fall through
write_string: ; zero-terminated, at DS:SI
.loop:
lodsb ; load string byte from DS:SI into AL
cmp al, 0 ; test for "NULL" 0 byte, signaling end of string
je .done ; jump out of loop if AL equaled 0
mov ah, 0xE ; function: write character in teletype mode
int 0x10 ; invoke video driver
jmp .loop
.done:
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; DS:SI=string pointer, CX=string length
write_string_sized:
cmp cx, 0 ; check if string length is zero
je .done ; abort if string length is zero
.loop:
lodsb ; load string byte into AL, from DS:SI
mov ah, 0Eh ; function: write character in teletype mode
int 10h ; invoke video driver
loop .loop ; decrement CX and loop
.done:
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Global string data; Uses suffix `_sz` for "string zero"
command_prompt_sz db "> ", 0
empty_sz db 0
error_sz db "ERROR!", 0
initial_video_mode_label_sz db "Initial video mode: ", 0
newline_sz db 13, 10, 0
os_title_sz db "8 6 / O S", 0
press_any_key_to_reset_sz db "Press any key to reset...", 0
unknown_command_sz db 13, 10, "Unknown command.", 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Verify kernel sector count (nasm error if larger)
times 512*2-($-$$) db 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;