Repository navigation
Expand file tree
/
Copy pathkernel.asm
More file actions
237 lines (197 loc) · 4 KB
/
Copy pathkernel.asm
File metadata and controls
237 lines (197 loc) · 4 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
BITS 16
[org 0x0000]
;%include "bootloader.asm"
mov ax, 0x1000
mov ds, ax
mov es, ax
mov al, 'K'
int 0x10
mov al, '>'
int 0x10
start:
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x7E00
call clear_screen
call show_startup_msg
main_loop:
call print_newline
mov si, prompt_string
call print_string
mov di, command_buffer
call read_string
mov si, command_buffer
mov di, info_cmd
call string_compare
je handle_info
mov si, command_buffer
mov di, help_cmd
call string_compare
je handle_help
mov si, command_buffer
mov di, clear_cmd
call string_compare
je handle_clear
mov si, unknown_cmd
call print_newline
call print_string
jmp main_loop
; ----------------------------------------------------------------
handle_info:
call print_newline
; Get base memory in KB using BIOS INT 12h
int 12h
mov cx, ax
; Print message
mov si, base_mem_msg
call print_string
; Print the number (AX is in KB)
mov ax, cx
call print_number
; Add " KB"
mov si, kb_msg
call print_string
call print_newline
jmp main_loop
; ----------------------------------------------------------------
handle_help:
call print_newline
mov si, help_menu_string
call print_string
call print_newline
jmp main_loop
; ----------------------------------------------------------------
handle_clear:
call clear_screen
call show_startup_msg
jmp main_loop
; ----------------------------------------------------------------
show_startup_msg:
mov si, welcome_string
call print_string
call print_newline
mov si, intro_string
call print_string
call print_newline
mov si, help_menu_string
call print_string
call print_newline
ret
; ----------------------------------------------------------------
print_string:
mov ah, 0Eh
.repeat:
lodsb
cmp al, 0
je .done
int 10h
jmp .repeat
.done:
ret
print_newline:
push ax
mov ah, 0Eh
mov al, 0Dh
int 10h
mov al, 0Ah
int 10h
pop ax
ret
; ----------------------------------------------------------------
print_number:
pusha
xor cx, cx
mov bx, 10
.next_digit:
xor dx, dx
div bx
push dx
inc cx
test ax, ax
jnz .next_digit
.print_digits:
pop dx
add dl, '0'
mov ah, 0Eh
mov al, dl
int 10h
loop .print_digits
popa
ret
; ----------------------------------------------------------------
read_string:
pusha
mov bx, di
.read_loop:
mov ah, 00h
int 16h
cmp al, 0Dh
je .done
cmp al, 08h
je .backspace
mov [di], al
mov ah, 0Eh
int 10h
inc di
jmp .read_loop
.backspace:
cmp di, bx
je .read_loop
dec di
mov byte [di], 0
mov ah, 0Eh
mov al, 08h
int 10h
mov al, ' '
int 10h
mov al, 08h
int 10h
jmp .read_loop
.done:
mov byte [di], 0
popa
ret
; ----------------------------------------------------------------
string_compare:
pusha
.loop:
mov al, [si]
mov ah, [di]
cmp al, ah
jne .notequal
cmp al, 0
je .equal
inc si
inc di
jmp .loop
.notequal:
popa
clc
ret
.equal:
popa
stc
ret
; ----------------------------------------------------------------
clear_screen:
pusha
mov ah, 0x00
mov al, 0x03
int 10h
popa
ret
; ----------------------------------------------------------------
; Strings and Buffers
welcome_string db 'Welcome to 16-bit shell', 0
intro_string db 'Commands: info, help, clear', 0
prompt_string db 'OS >> ', 0
unknown_cmd db 'Unknown command', 0
base_mem_msg db 'Base Memory: ', 0
kb_msg db ' KB', 0
info_cmd db 'info', 0
help_cmd db 'help', 0
clear_cmd db 'clear', 0
help_menu_string db 'info - Show base memory', 0Dh, 0Ah, 'clear - Clear screen', 0Dh, 0Ah, 'help - Show this menu', 0Dh, 0Ah, 0
command_buffer times 64 db 0