-
Notifications
You must be signed in to change notification settings - Fork 0
/
kern.asm
324 lines (302 loc) · 4.27 KB
/
kern.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
%include "defs.asm"
[org 0x7e00]
[bits 16]
; determine which function to execute
cmp ah, 0xff
je init
cmp ah, 0x01
je printffunc
cmp ah, 0x02
je printfsfunc
cmp ah, 0x03
je strcmpfunc
cmp ah, 0x04
je readstrfunc
cmp ah, 0x05
je loadprogfunc
cmp ah, 0x06
je clearscreenfunc
cmp ah, 0x07
je zerobufferfunc
cmp ah, 0x08
je listthingsfunc
cmp ah, 0x09
je setmainprogfunc
cmp ah, 0x0A
je retmainfunc
; load init program and run it
init:
mov si, initstr
jmp loadprogfunc
loadprogfunc:
mov bx, fdt
loadprogloop:
mov [sibuff], si
mov [bxbuff], bx
add bx, 4
mov ch, 4
call strcmpfunc
mov bx, [bxbuff]
cmp ah, 0
je loadprogend
mov si, [sibuff]
add bx, 9
jmp loadprogloop
loadprogend:
mov ah, 2
mov al, [bx + 3]
mov ch, 0
mov cl, [bx + 8]
mov dh, 0
mov dl, 0x80
mov bx, userloc
int 0x13
jmp userloc
; print string
printffunc:
mov ah, 0x0e
printfloop:
mov al, [bx]
cmp al, 0
je printfend
cmp al, '%'
je printfformat
cmp al, 10
je printfnewline
int 10h
printfdone:
inc bx
jmp printfloop
;return from printf
printfend:
ret
;print a newline
printfnewline:
mov al, 0xA
int 10h
mov al, 0xD
int 10h
je printfdone
; process formatters
printfformat:
inc bx
mov al, [bx]
cmp al, 'c'
je printchar
cmp al, 'd'
je printint
jmp printfdone
; print a charachter in ch
printchar:
mov al, ch
int 10h
je printfdone
; print an integer
; set up loop
printint:
cmp ch, 10
jl printintsmall
mov cl, 0
printintloop:
cmp ch, 10
mov al, '1'
je printintend
cmp ch, 20
mov al, '2'
je printintend
cmp ch, 30
mov al, '3'
je printintend
cmp ch, 40
mov al, '4'
je printintend
cmp ch, 50
mov al, '5'
je printintend
cmp ch, 60
mov al, '6'
je printintend
cmp ch, 70
mov al, '7'
je printintend
cmp ch, 80
mov al, '8'
je printintend
cmp ch, 90
mov al, '9'
je printintend
dec ch
inc cl
jmp printintloop
printintend:
; print final result
mov ah, 0x0e
int 10h
mov al, cl
add al, '0'
int 10h
mov ah, 0x0e
jmp printfdone
printintsmall:
mov al, ch
mov ah, 0x0e
add al, '0'
int 10h
jmp printfdone
; prints a string with a fixed size
printfsfunc:
mov ah, 0x0e
printfsloop:
cmp ch, 0
je printfsend
mov al, [bx]
cmp al, 0
je printfsend
int 10h
dec ch
inc bx
jmp printfsloop
printfsend:
ret
; compares 2 strings, uses dx
strcmpfunc:
cmp ch, 0
je strcmpsame
mov dh, [bx]
mov dl, [si]
cmp dh, dl
jne strcmpnotsame
cmp dh, 0
je strcmpsame
cmp dl, 0
je strcmpsame
inc bx
inc si
dec ch
jmp strcmpfunc
strcmpsame:
mov ah, 0
ret
strcmpnotsame:
mov ah, 1
ret
readstrfunc:
mov ah, 0
add cx, bx
sub cx, 1
mov dx, bx
readstrloop:
mov ah, 0
int 16h
cmp al, 8
je readstrbs
cmp al, 13
je readstrend
mov ah, 0x0e
int 10h
mov [bx], al
cmp bx, cx
je readstrend
inc bx
jmp readstrloop
readstrbs:
cmp bx, dx
je readstrloop
mov ah, 0x0e
int 10h
mov al, ' '
int 10h
mov al, 8
int 10h
dec bx
mov BYTE [bx], 0
jmp readstrloop
readstrend:
ret
clearscreenfunc:
mov ah, 0x0e
mov bl, 200
clearscreenloop:
cmp bl, 0
je clearscreenend
mov ah, 0x0e
mov al, 0xA
int 10h
mov al, 0xD
int 10h
dec bl
jmp clearscreenloop
clearscreenend:
mov ah, 0x02
mov bh, 0
mov dh, 0
mov dl, 0
int 10h
ret
zerobufferfunc:
mov dx, bx
add dx, cx
zerobufferloop:
cmp bx, dx
je zerobufferend
mov BYTE [bx], 0
inc bx
jmp zerobufferloop
zerobufferend:
ret
listthingsfunc:
mov ah, 0x0e
mov al, 0xA
int 10h
mov al, 0xD
int 10h
mov bx, fdt
mov [bxbuff], bx
listthingsloop:
mov bx, [bxbuff]
mov ah, [bx + 1]
cmp ah, 0
je listthingsend
mov ah, [bx]
cmp ah, 0
je listthingsdeleted
cmp ah, 3
je listthingsdeleted
add bx, 4
mov ch, 4
call printfsfunc
mov ah, 0x0e
mov al, 0xA
int 10h
mov al, 0xD
int 10h
listthingsdeleted:
mov bx, [bxbuff]
add bx, 9
mov [bxbuff], bx
jmp listthingsloop
listthingsend:
ret
setmainprogfunc:
mov si, mainprog
mov cl, 4
setmainprogloop:
cmp cl, 0
je setmainprogend
mov ch, [bx]
mov [si], ch
inc bx
inc si
dec cl
jmp setmainprogloop
setmainprogend:
ret
retmainfunc:
mov si, mainprog
jmp loadprogfunc
bxbuff: dw 0
sibuff: dw 0
fmt: db "%d", 10, 0
initstr: db "init"
mainprog: db " "
times 1024-($-$$) db 0