-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboot32.lst
More file actions
303 lines (303 loc) · 17.6 KB
/
Copy pathboot32.lst
File metadata and controls
303 lines (303 loc) · 17.6 KB
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
1 ;
2 ; A Simple Boot sector to print a message
3 ;
4
5
6 [org 0x7c00]
7
8 ; Start Up
9
10 00000000 8816[2E01] mov [BOOT_DRIVE], dl ; BIOS stores our boot drive in DL,so it’s best to remember this for later.
11
12 00000004 BE[1B01] mov si, BOOT_MSG
13 00000007 E81200 call printString
14 ; mov cl,0x02 ; two byte value
15 ; mov edx,0x1234
16 ; call printHex
17 ; mov si, NEW_LINE
18 ; call printString
19
20
21
22 ; read from disk
23
24 ; mov bp,0x8000 ; set stack safely out of the way
25 ; mov sp, bp
26
27 ; mov bx, 0x9000 ; Load 5 sectors to 0x0000(ES):0x9000(BX) from boot disk
28 ; mov dh, 5
29 ; mov dl, [BOOT_DRIVE]
30 ; call diskLoad
31
32 ; mov dx, [0x9000] ; PRIN TTH EFIRST LOADED WORD
33 ; mov cx,2 ; print two bytes
34 ; call printHex
35
36 ; mov dx, [0x9000 + 512] ; prin tdata that came form 2nd sector
37 ; mov cx,2
38 ; call printHex
39 0000000A EB00 jmp end
40
41
42 end:
43 ; Prepare for 32 bit protected mode
44
45
46 0000000C BD0090 mov bp, 0x9000 ; set the stack
47 0000000F 89EC mov sp,bp
48
49 00000011 BE[3901] mov si, MSG_REAL_MODE
50 00000014 E80500 call printString
51
52 00000017 E8B700 call switch_to_pm
53
54 0000001A EBFE jmp $ ; We should never get here
55
56
57
58
59
60 ; Includes
61 %include "print.asm"
1 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 <1> ; Prints null terminated string
3 <1> ; Put first letter into SI
4 <1> ; Set BX to number of bytes to use
5 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6 <1> printString:
7 0000001C AC <1> lodsb ; Load the byte at address in SI to AL and Inc SI
8 0000001D 3C00 <1> cmp al,0 ; check for end of line
9 0000001F 7405 <1> je printStringEnd
10 00000021 E80300 <1> call printChar
11 00000024 EBF6 <1> jmp printString
12 <1> printStringEnd:
13 00000026 C3 <1> ret
14 <1>
15 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16 <1> ; Prints a single character to the BIOS Teletype function
17 <1> ; Put char in AL
18 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19 <1> printChar:
20 <1> ; Call BIOS Routing to print
21 00000027 50 <1> push ax
22 00000028 53 <1> push bx
23 00000029 B40E <1> mov ah, 0x0e ; BIOS Teletype function
24 0000002B B700 <1> mov bh,0 ; Page 0
25 0000002D B347 <1> mov bl, 0x47 ; Text Attribute (light grey on black)
26 0000002F CD10 <1> int 0x10 ; call BIOS intterupt 0x10
27 00000031 5B <1> pop bx
28 00000032 58 <1> pop ax
29 00000033 C3 <1> ret
30 <1>
31 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 <1> ; Prints a byte or word in hex format
33 <1> ; Assumes value is in DX and can be 32 bit value
34 <1> ; Set BX to number of bytes to use
35 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 <1> printHex:
37 00000034 6651 <1> push ecx ; save cl
38 00000036 B004 <1> mov al,4 ; load number of bytes for full 32 bit number
39 00000038 28C8 <1> sub al,cl ; get number of byte to shift out for alignment
40 0000003A B108 <1> mov cl,8 ; set bit multiplier
41 0000003C F6E1 <1> mul cl ; multiply by byute count - we now have numebr of bits to shift right
42 0000003E 88C1 <1> mov cl,al
43 00000040 66D3CA <1> ror edx,cl
44 00000043 6659 <1> pop ecx ; restore cl
45 00000045 D0E1 <1> shl cl,1 ; multiply by 2 for nibbles
46 <1> ; todo: could put check in for a value greater than 4
47 <1> hexLoop:
48 00000047 80F900 <1> cmp cl,0 ; if bx = 0 then exit
49 0000004A 7416 <1> jz hexFin
50 0000004C 66C1C204 <1> rol edx,4 ; Rotate the word to get first byte ready
51 00000050 49 <1> dec cx
52 <1> ; Swap
53 00000051 88D0 <1> mov al,dl ; copy byte into AL
54 00000053 240F <1> and al,0x0f ; clear high nibble
55 <1>
56 <1> ; Handle al first
57 00000055 3C0A <1> cmp al,0x0a
58 00000057 7202 <1> jb nibLess
59 00000059 0407 <1> add al,0x07 ; add offset for Hex letters
60 <1> nibLess:
61 0000005B 0430 <1> add al,0x30
62 0000005D E8C7FF <1> call printChar
63 00000060 EBE5 <1> jmp hexLoop
64 <1> hexFin:
65 00000062 C3 <1> ret
66 <1>
67 <1>
68 00000063 0A0D00 <1> NEW_LINE: db 0xa, 0xd,0
62 %include "disk.asm"
1 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 <1> ; Function to read from the disk
3 <1> ;load DHsectors to ES:BX from drive DL
4 <1> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 <1>
6 <1> diskLoad:
7 00000066 52 <1> push dx
8 <1>
9 00000067 B402 <1> mov ah,0x02 ; BIOS read sector function
10 00000069 88F0 <1> mov al,dh ; Read DH sectors
11 0000006B B500 <1> mov ch,0x00 ; Select cylinder 0
12 0000006D B600 <1> mov dh,0x00
13 0000006F B102 <1> mov cl,0x02 ; Start reading from second sector - ie after boot sector
14 <1>
15 00000071 CD13 <1> int 0x13 ; BIOS interrupt
16 <1>
17 00000073 7206 <1> jc disk_error_BIOS
18 <1>
19 00000075 5A <1> pop dx
20 00000076 38C6 <1> cmp dh,al ; if AL (sectors read) != DH (sectors ecpected)
21 00000078 7509 <1> jne disk_error_SECT
22 0000007A C3 <1> ret
23 <1>
24 <1> disk_error_BIOS:
25 0000007B BE[8B00] <1> mov si, DISK_ERROR_BIOS
26 0000007E E89BFF <1> call printString
27 00000081 EBFE <1> jmp $
28 <1>
29 <1> disk_error_SECT:
30 00000083 BE[9E00] <1> mov si, DISK_ERROR_SECT
31 00000086 E893FF <1> call printString
32 00000089 EBFE <1> jmp $
33 <1>
34 <1> ; Variables
35 0000008B 4469736B2042494F53- <1> DISK_ERROR_BIOS: db "Disk BIOS error!", 0xa, 0xd, 0
35 00000094 206572726F72210A0D- <1>
35 0000009D 00 <1>
36 0000009E 4469736B2073656374- <1> DISK_ERROR_SECT: db "Disk sector error!", 0xa, 0xd, 0
36 000000A7 6F72206572726F7221- <1>
36 000000B0 0A0D00 <1>
63 %include "gdt.asm"
1 <1> ; GDT
2 <1>
3 <1> gdt_start:
4 <1>
5 <1> gdt_null: ;the madatory null descriptor
6 000000B3 00000000 <1> dd 0x0
7 000000B7 00000000 <1> dd 0x0
8 <1>
9 <1> gdt_code : ; The code segment descriptor
10 <1>
11 000000BB FFFF <1> dw 0xffff ; Limite (bits 0-15)
12 000000BD 0000 <1> dw 0x0 ; Base (bits 0-15)
13 000000BF 00 <1> db 0x0 ; Base (bits 16-23)
14 000000C0 9A <1> db 10011010b ; 1st flags, type flags
15 000000C1 CF <1> db 11001111b ; 2nd flags, limit (bits 16-19)
16 000000C2 00 <1> db 0x0 ; Base (bits 24-31)
17 <1>
18 <1>
19 <1> gdt_data: ; The data segment desciptor
20 <1>
21 000000C3 FFFF <1> dw 0xffff ; Limit (bits 0-15)
22 000000C5 0000 <1> dw 0x0 ; Base (bits 0-15)
23 000000C7 00 <1> db 0x0 ; Base (bits 16-23)
24 000000C8 92 <1> db 10010010b ; 1st flags, type flags
25 000000C9 CF <1> db 11001111b ; 2nd flags. Limit (bits 16-19)
26 000000CA 00 <1> db 0x0 ; Base (bits 24-31)
27 <1>
28 <1> gdt_end:
29 <1>
30 <1>
31 <1> ; GDT Descriptor
32 <1> gdt_descriptor:
33 000000CB 1700 <1> dw gdt_end - gdt_start -1
34 000000CD [B3000000] <1> dd gdt_start
35 <1>
36 <1>
37 <1> CODE_SEG equ gdt_code - gdt_start
38 <1> DATA_SEG equ gdt_data - gdt_start
39 <1>
40 <1>
64 %include "switchPM.asm"
1 <1> [bits 16]
2 <1>
3 <1> ; Switch tp protected mode
4 <1>
5 <1> switch_to_pm:
6 000000D1 FA <1> cli
7 <1>
8 000000D2 0F0116[CB00] <1> lgdt [gdt_descriptor]
9 <1>
10 000000D7 0F20C0 <1> mov eax,cr0
11 000000DA 6683C801 <1> or eax, 0x1
12 000000DE 0F22C0 <1> mov cr0,eax
13 <1>
14 000000E1 EA[E600]0800 <1> jmp CODE_SEG:init_pm
15 <1>
16 <1>
17 <1> [bits 32]
18 <1> ; Initialise registers and the stack once in PM
19 <1> init_pm:
20 000000E6 66B81000 <1> mov ax, DATA_SEG ; Move old Segments to DATA_SEG defined in GDT
21 000000EA 8ED8 <1> mov ds, ax
22 000000EC 8ED0 <1> mov ss, ax
23 000000EE 8EC0 <1> mov es, ax
24 000000F0 8EE0 <1> mov fs, ax
25 000000F2 8EE8 <1> mov gs, ax
26 <1>
27 000000F4 BD00000900 <1> mov ebp, 0x90000 ; Update stack point to top of free space
28 000000F9 89EC <1> mov esp, ebp
29 <1>
30 000000FB E82F000000 <1> call BEGIN_PM
31 <1>
65 %include "print_string_pm.asm"
1 <1> [bits 32]
2 <1>
3 <1> VIDEO_MEMORY equ 0xb8000
4 <1> WHITE_ON_BLACK equ 0x0f
5 <1>
6 <1> print_string_pm:
7 00000100 60 <1> pusha
8 00000101 BA00800B00 <1> mov edx, VIDEO_MEMORY
9 <1>
10 <1> print_string_pm_loop:
11 00000106 8A03 <1> mov al, [ebx]
12 00000108 B40F <1> mov ah, WHITE_ON_BLACK
13 <1>
14 0000010A 3C00 <1> cmp al, 0
15 0000010C 740B <1> je print_string_pm_done
16 <1>
17 0000010E 668902 <1> mov [edx], ax
18 <1>
19 00000111 83C301 <1> add ebx, 1
20 00000114 83C202 <1> add edx, 2
21 <1>
22 00000117 EBED <1> jmp print_string_pm_loop
23 <1>
24 <1> print_string_pm_done:
25 00000119 61 <1> popa
26 0000011A C3 <1> ret
66 ;GLOBAL VERIABLES
67 0000011B 4C6F6164696E67206F- BOOT_MSG: db 'Loading osGG-x86', 0xa, 0xd, 0
67 00000124 7347472D7838360A0D-
67 0000012D 00
68 0000012E 00 BOOT_DRIVE: db 0
69
70
71 [bits 32]
72
73 BEGIN_PM:
74 0000012F BB[57010000] mov ebx, MSG_PROT_MODE
75 00000134 E8C7FFFFFF call print_string_pm
76
77 ; Global Veriables
78 00000139 537461727465642069- MSG_REAL_MODE: db "Started in 16 bit Real Mode", 0xd,0xa,0
78 00000142 6E2031362062697420-
78 0000014B 5265616C204D6F6465-
78 00000154 0D0A00
79 00000157 53756363657373202D- MSG_PROT_MODE: db "Success - We are in 32 bit protected mode",0xd,0xa,0
79 00000160 205765206172652069-
79 00000169 6E2033322062697420-
79 00000172 70726F746563746564-
79 0000017B 206D6F64650D0A00
80
81 ; Padding Bios and Magic number
82
83 00000183 00<rep 7Bh> times 510-($-$$) db 0
84
85 000001FE 55AA dw 0xaa55
86
87 00000200 DADA<rep 100h> times 256 dw 0xdada
88 00000400 CEFA<rep 100h> times 256 dw 0xface
89