-
Notifications
You must be signed in to change notification settings - Fork 0
/
micro-os_loader.asm
179 lines (133 loc) · 4.11 KB
/
micro-os_loader.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
name "loader"
; this is a very basic example of a tiny operating system.
; directive to create boot file:
#make_boot#
; this is an os loader only!
;
; it can be loaded at the first sector of a floppy disk:
; cylinder: 0
; sector: 1
; head: 0
;=================================================
; how to test micro-operating system:
; 1. compile micro-os_loader.asm
; 2. compile micro-os_kernel.asm
; 3. compile writebin.asm
; 4. insert empty floppy disk to drive a:
; 5. from command prompt type:
; writebin loader.bin
; writebin kernel.bin /k
;=================================================
;
; The code in this file is supposed to load
; the kernel (micro-os_kernel.asm) and to pass control over it.
; The kernel code should be on floppy at:
; cylinder: 0
; sector: 2
; head: 0
; memory table (hex):
; -------------------------------
; 07c0:0000 | boot sector
; 07c0:01ff | (512 bytes)
; -------------------------------
; 07c0:0200 | stack
; 07c0:03ff | (255 words)
; -------------------------------
; 0800:0000 | kernel
; 0800:1400 |
; | (currently 5 kb,
; | 10 sectors are
; | loaded from
; | floppy)
; -------------------------------
; To test this program in real envirinment write it to floppy
; disk using compiled writebin.asm
; After sucessfully compilation of both files,
; type this from command prompt: writebin loader.bin
; Note: floppy disk boot record will be overwritten.
; the floppy will not be useable under windows/dos until
; you reformat it, data on floppy disk may be lost.
; use empty floppy disks only.
; micro-os_loader.asm file produced by this code should be less or
; equal to 512 bytes, since this is the size of the boot sector.
; boot record is loaded at 0000:7c00
org 7c00h
; initialize the stack:
mov ax, 07c0h
mov ss, ax
mov sp, 03feh ; top of the stack.
; set data segment:
xor ax, ax
mov ds, ax
; set default video mode 80x25:
mov ah, 00h
mov al, 03h
int 10h
; print welcome message:
lea si, msg
call print_string
;===================================
; load the kernel at 0800h:0000h
; 10 sectors starting at:
; cylinder: 0
; sector: 2
; head: 0
; BIOS passes drive number in dl,
; so it's not changed:
mov ah, 02h ; read function.
mov al, 10 ; sectors to read.
mov ch, 0 ; cylinder.
mov cl, 2 ; sector.
mov dh, 0 ; head.
; dl not changed! - drive number.
; es:bx points to receiving
; data buffer:
mov bx, 0800h
mov es, bx
mov bx, 0
; read!
int 13h
;===================================
; integrity check:
cmp es:[0000],0E9h ; first byte of kernel must be 0E9 (jmp).
je integrity_check_ok
; integrity check error
lea si, err
call print_string
; wait for any key...
mov ah, 0
int 16h
; store magic value at 0040h:0072h:
; 0000h - cold boot.
; 1234h - warm boot.
mov ax, 0040h
mov ds, ax
mov w.[0072h], 0000h ; cold boot.
jmp 0ffffh:0000h ; reboot!
;===================================
integrity_check_ok:
; pass control to kernel:
jmp 0800h:0000h
;===========================================
print_string proc near
push ax ; store registers...
push si ;
next_char:
mov al, [si]
cmp al, 0
jz printed
inc si
mov ah, 0eh ; teletype function.
int 10h
jmp next_char
printed:
pop si ; re-store registers...
pop ax ;
ret
print_string endp
;==== data section =====================
msg db "Loading...",0Dh,0Ah, 0
err db "invalid data at sector: 2, cylinder: 0, head: 0 - integrity check failed.", 0Dh,0Ah
db "refer to tutorial 11 - making your own operating system.", 0Dh,0Ah
db "System will reboot now. Press any key...", 0
;======================================