-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.asm
278 lines (235 loc) · 4.53 KB
/
kernel.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
[bits 16]
[org 0x7e00]
section .text
mov si, msg
call print
mov ax, 0
mov ds, ax
mov si, idt
lidt [si]
cli
mov al, 0x20
out 0x21, al
sti
mov si, prompt
call print_prompt
command_loop:
mov ah, 0x00
int 0x16
mov [key_pressed], al
cmp al, 0x0D
je process_command
mov ah, 0x0e
int 0x10
jmp command_loop
process_command:
call handle_command
jmp command_loop
print:
lodsb
or al, al
jz done
mov ah, 0x0e
int 0x10
jmp print
done:
ret
print_prompt:
lodsb
or al, al
jz done_prompt
mov ah, 0x0e
int 0x10
jmp print_prompt
done_prompt:
ret
handle_command:
mov si, command_buffer
call execute_command
mov si, prompt
call print_prompt
ret
execute_command:
mov di, echo_command
call compare_command
jc echo_found
mov di, read_command
call compare_command
jc read_found
mov di, write_command
call compare_command
jc write_found
jmp command_not_found
echo_found:
; Execute echo command
mov si, echo_message
call print_prompt
mov si, si_command_buffer
call print_prompt
ret
read_found:
; Execute read command
compare_command:
push si
push di
mov cx, 4
cld
repe cmpsb
je .match
clc
jmp .done
.match:
stc
.done:
pop di
pop si
ret
write_found:
mov si, write_message
call print_prompt
mov si, si_command_buffer
call write_file
ret
compare_command:
mov cx, 0
cld
rep cmpsb
ret
msg db 'Welcome to CheeseOS!', 0
prompt db '>', 0
echo_command db 'echo', 0
echo_message db 'You typed: ', 0
read_command db 'read', 0
read_message db 'File content:', 0
write_command db 'write', 0
write_message db 'Writing to file...', 0
section .data
idt:
dw 256 * 8 - 1 ; IDT limit
dd idt_entries ; IDT base address
section .text
idt_entries:
times 256 dd isr_default ; Fill IDT with default handler addresses
isr_default:
cli
hlt
section .bss
key_pressed resb 1 ; Reserve space to store pressed key
command_buffer resb 64 ; Reserve space for command buffer
si_command_buffer resb 64 ; Reserve space for command buffer
file_content resb 1024 ; Reserve space for file content
section .data
file_system:
directory:
file1 db 'File1', 0
file2 db 'File2', 0
file1_content db 'Content of File1', 0
file2_content db 'Content of File2', 0
read_file:
mov cx, 0
mov dx, si ; Move file name pointer to DX
mov si, file_system
mov di, directory
jmp compare_file_names
find_file:
mov cx, 0
mov dx, si ; Move file name pointer to DX
mov si, di
call compare_file_names
compare_file_names:
lodsb
cmp al, [dx]
jne next_file
cmp al, 0
je file_found
inc cx
jmp compare_file_names
next_file:
lodsb
cmp al, 0
jne next_file
mov si, di
add si, cx
mov cx, 0
lodsb
cmp al, 0
je no_file_found
add si, cx
jmp find_file
file_found:
lodsb
cmp al, 0
jne file_found
mov si, file_system
add si, 32
mov cx, 0
jmp read_file_content
read_file_content:
lodsb
or al, al
jz file_content_read
stosb
jmp read_file_content
file_content_read:
ret
no_file_found:
mov si, no_file_message
call print_prompt
ret
no_file_message db 'File not found!', 0
write_file:
mov cx, 0
mov dx, si
mov si, file_system
mov di, directory
jmp compare_file_names_write
find_file_write:
mov cx, 0
mov dx, si
mov si, di
call compare_file_names_write
compare_file_names_write:
lodsb
cmp al, [dx]
jne next_file_write
cmp al, 0
je file_found_write
inc cx
jmp compare_file_names_write
next_file_write:
lodsb
cmp al, 0
jne next_file_write
mov si, di
add si, cx
mov cx, 0
lodsb
cmp al, 0
je no_file_found_write
add si, cx
jmp find_file_write
file_found_write:
lodsb
cmp al, 0
jne file_found_write
mov si, file_system
add si, 32
mov cx, 0
jmp write_file_content
write_file_content:
lodsb
or al, al
jz file_content_written
stosb
jmp write_file_content
file_content_written:
ret
no_file_found_write:
mov si, no_file_message
call print_prompt
ret
command_not_found:
mov si, unknown_command
call print_prompt
ret
unknown_command db 'Unknown command!', 0