-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_boot_sector
227 lines (204 loc) · 3.61 KB
/
example_boot_sector
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
org 0x7c00 ; Tell assembler to correct the offsets
;
; Main method
; Prints welcome and exit messages
;
;mov bx, HELLO_MSG
;call PRINT_NWLN
;
mov bx, PRINTING_HEX_MSG_1
call PRINT
mov dx, 0x1fb6
call PRINT_HEX_NWLN
mov bx, PRINTING_HEX_MSG_2
call PRINT
mov dx, 0xdead
call PRINT_HEX_NWLN
;
;mov ax, 0x4000
;mov bx, 0
;mov es, ax
;mov dh, 2
;call READ_DISK
;
;mov bx, GOODBYE_MSG
;call PRINT
jmp $ ; Hang
;
; Print Newline
; Wrapper for print function
; Appends newline
;
PRINT_NWLN:
call PRINT
push bx
mov bx, NWLN
call PRINT
pop bx
ret
;
; Print function
; Takes parameters via bx
; Returns nothing
; Preserves registers
;
; Prints out the null terminated
; string stored at address in bx
;
PRINT:
pusha
mov ah, 0x0e ; scrolling teletype bios routine
.eval_char:
mov cx, [bx]
test cl, cl ; check to see if string has terminated
je .done
mov al, cl
int 0x10 ; interrupt prints to screen
add bx, 1 ; move to the next character
jmp .eval_char
.done:
popa
ret
;
; Wrapper for PrintHex
; Appends newline
;
PRINT_HEX_NWLN:
call PRINT_HEX
push bx
mov bx, NWLN
call PRINT
pop bx
ret
;
; PrintHex function
; Takes parameters via dx
; Returns nothing
; Preserves registers
; Uses global HEX_VALUE
;
; Prints out the hexidecimal
; stored in the dx register
;
PRINT_HEX:
pusha ; setup registers
mov ax, 0xf000
mov cl, 12
mov bx, HEX_VALUE
add bx, 2
.adjust_hex_value:
push dx ; save original value for later use
and dx, ax ; isolate four appropriate bits
shr dx, cl
cmp dx, 0xa
jl .was_just_digit
add dx, 39 ; some number voodoo for ascii
.was_just_digit:
add [bx], dx ; edit character the bytes in bx represent
shr ax, 4 ; increment to next bits
sub cl, 4
add bx, 1
pop dx
test ax, ax
jne .adjust_hex_value
mov bx, HEX_VALUE
call PRINT
mov bx, HEX_VALUE
add bx, 2
mov cx, '0'
mov [bx], cx
add bx, 1
mov [bx], cx
add bx, 1
mov [bx], cx
add bx, 1
mov [bx], cx
add bx, 1
popa
ret
HEX_VALUE:
db '0x0000',0
;
; ReadDisk option
; Takes number of sectors to read via dh
; Returns nothing
; Does not preserve registers
; Reads in a number of sectors from
; the boot medium, immediately following
; the boot sector itself
; Loads the sectors to ES:BX
;
;
;READ_DISK:
;
; push bx ; inform user of disk read
; mov bx, DISK_LOADING_MSG
; call PRINT_NWLN
; pop bx
;
; push dx
; mov ah, 0x02 ; BIOS read sector function
; mov al, dh ; reads DH sectors
; mov ch, 0x00 ; cylinder 0
; mov dh, 0x00 ; head 0
; mov cl, 0x02 ; sector 2
;
; int 0x013
;
; jc .disk_error ; if carry flag set, jump to error handler
;
; pop dx ; restore original dx
; cmp dh, al ; if sectors read != sectors requested:
; jne .disk_error ; jump to error handler
;
; mov bx, DISK_SUCCESS_MSG
; call PRINT_NWLN
;
; ret
;.disk_error:
; mov bx, DISK_ERROR_MSG_1 ; inform user of failure
; call PRINT
; mov bh, 0
; mov bl, al
; call PRINT_HEX
; mov bx, DISK_ERROR_MSG_2
; call PRINT_NWLN
; jmp $ ; Hang on error
;
;
; Globals
;
NWLN:
db 0xa,0xd,0
PRINTING_HEX_MSG_1:
db 'Currently printing a hex number...Should be 0x1fb6: ',0
PRINTING_HEX_MSG_2:
db 'Currently printing a hex number...Should be 0x9c1a: ',0
;DISK_LOADING_MSG:
; db 'Reading sectors from disk...',0
;
;DISK_ERROR_MSG_1:
; db 'Disk read error!',0xa,0xd,'Read failed after ',0
;
;DISK_ERROR_MSG_2:
; db ' sectors were read',0
;
;DISK_SUCCESS_MSG:
; db 'Sectors read to memory',0
;
;HELLO_MSG:
; db 'Salutations. Welcome to NimbOS.',0
;
;GOODBYE_MSG:
; db 'Farewell. Until next time.',0
;
;
; Magic number and padding
;
times 510-($-$$) db 0
dw 0xaa55
;
; Added a byte of gibberish to read from
;
times 256 dw 0xabab
times 256 dw 0xcdcd