-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootloader_stage1.asm
169 lines (133 loc) · 2.46 KB
/
bootloader_stage1.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
;==================================
; KevinOS bootloader 2
; Written March 28, 2014
;==================================
ORG 0x7C00
jmp 0x0000:start
; Start execution
start:
;xor ax, ax
mov ax, 0
mov ds, ax
mov es, ax
; Set up the stack:
cli
mov bp, 0x7C00
mov sp, bp
mov ss, ax
sti
; Store the boot drive for later:
mov [BOOT_DRIVE], dl
; Detect Low Memory (below 1Mb, usually returns < 640Kb)
xor ax, ax
int 0x12
jc .mem_error
jmp .mem_continue
.mem_error:
jmp $
.mem_continue:
; ax now contains the # of kB from zero of continuous memory
mov bx, ax
call printint
mov bx, MSG_KB
call println
; Wait for keystroke
.wait:
mov bx, MSG_2
call println
mov ah, 0
int 0x16
; Load the second stage
; load AL sectors to ES:BX from drive DL
mov bx, 0x7C00 + 512
mov dl, [BOOT_DRIVE]
push dx
mov ah, 0x02 ; 0x13 read sector
mov al, 1 ; how many sectors to read
mov ch, 0 ; cylinder 0
mov dh, 0 ; head 0
mov cl, 2 ; start at sector 2 (1 is boot sector)
int 0x13
jc .disk_error
pop dx
cmp al, 1
jne .incomplete
jmp .stage2
.disk_error:
mov bx, ERROR1
call println
call halt
.incomplete:
mov bx, ERROR2
call println
call halt
.stage2:
mov bx, MSG_STAGE2
call println
mov bx, 0x07c0
mov es, bx
mov bx, 0x200
push es
push bx
retf
jmp $
; Functions-------------------------------
; string address in bx
println:
call print
mov al, 0x0D ; LF CR
int 0x10
mov al, 0x0A
int 0x10
ret
print:
mov ah, 0x0e ; int 0x10 teletype output
.print_repeat:
mov al, [bx]
cmp al, 0
je .print_done
int 0x10
inc bx
jmp .print_repeat
.print_done:
ret
halt:
mov bx, HALTING
call println
jmp $
; int in bx, must be 000 - 999
printint:
push dx
push bx
push ax
xor dx, dx
mov ax, bx
mov bx, 100
div bx ;divide dx:ax by bx, store in ax, remainder in dx
mov [NUM_BUFFER], al
add [NUM_BUFFER], byte '0'
mov ax, dx
xor dx, dx
mov bx, 10
div bx
mov [NUM_BUFFER+1], al
add [NUM_BUFFER+1], byte '0'
mov [NUM_BUFFER+2], dx
add [NUM_BUFFER+2], byte '0'
mov bx, NUM_BUFFER
call print
pop ax
pop bx
pop dx
ret
BOOT_DRIVE db 0
MSG_2 db 'Press a key to continue', 0
ERROR1 db 'Error1', 0
ERROR2 db 'Error2', 0
HALTING db 'Halting', 0
NUM_BUFFER db '000', 0
MSG_KB db ' kb RAM', 0
MSG_STAGE2 db 'Jumping to second stage', 0
times 510-($-$$) db 0
dw 0xaa55
;====================================================================