33; Written March 28, 2014
44;==================================
55
6- ORG 0x7C00
7- jmp 0x0000 :start
6+ ORG 0x7E00
7+ jmp 0x0000 :start
88
9+ ;BIOS_DATA_AREA equ 0x400
910 KERNEL_ADDRESS equ 0x200
1011 NUM_SECTORS equ 57
1112
1819 dw 0xffff ; Limit (bits 0-15)
1920 dw 0x0 ; Base (bits 0-15)
2021 db 0x0 ; Base (bits 16-23)
21- db 10011010b ; 1st flags, type flags
22+ db 10011010b ; 1st flags, access byte
2223 db 11001111b ; 2nd flags, Limit (bits 16-19)
2324 db 0x0 ; Base (bits 24-31)
2425 gdt_data:
4647; Set up the stack:
4748
4849 cli
49- mov bp , 0x9000
50+ mov bp , 0x7C00
5051 mov sp , bp
5152 mov ss , ax
5253 sti
5556
5657 mov [ BOOT_DRIVE ], dl
5758
58- ; Print out messages
59+ ; Detect Low Memory (below 1Mb, usually below 640Kb)
5960
60- mov bx , MSG_1
61- call println
62- mov bx , MSG_2
63- call println
61+ xor ax , ax
62+ int 0x12
63+ jc .mem_error
64+ ; ax now contains the # of kB from zero of continuous memory
65+ mov bx , ax
66+ call printint
67+ mov bx , MSG_KB
68+ call println
69+ jmp . wait
6470
65- ; Wait for keystroke
71+ .mem_error:
72+ jmp $
6673
74+ ; Wait for keystroke
75+ . wait :
76+ mov bx , MSG_2
77+ call println
6778 mov ah , 0
6879 int 0x16
6980
7081; Load the kernel
7182
72- mov bx , MSG_3
83+ mov bx , MSG_READING
84+ call print
85+ mov bx , NUM_SECTORS
86+ call printint
87+ mov bx , MSG_FROMDISK
7388 call println
7489
7590 mov bx , KERNEL_ADDRESS
7691 mov dh , NUM_SECTORS
7792 mov dl , [ BOOT_DRIVE ]
7893
7994; load DH sectors to ES:BX from drive DL
80-
95+ ; Add a looping thing here to load one sector at a time, rather than all at once
8196 push dx
8297 mov ah , 0x02 ; 0x13 read sector
83- mov al , dh
98+ ;.load_loop:
99+ mov al , dh ; how many sectors to read
84100 mov ch , 0 ; cylinder 0
85101 mov dh , 0 ; head 0
86- mov cl , 2 ; start at sector 2 (1 is boot sector)
102+ mov cl , 3 ; start at sector 2 (1 is boot sector)
87103 int 0x13
104+
88105 jc .disk_error
89106 pop dx
90107 cmp dh , al
99116 call println
100117 call halt
101118.incomplete:
102- mov bx , INCOMPLETE_READ
119+ mov bx , DISK_ERROR
120+ call println
121+ mov bx , MSG_READ
122+ call print
123+ mov bl , dh
124+ call printint
125+ mov bx , MSG_FROMDISK
103126 call println
104127 call halt
105128
@@ -113,6 +136,10 @@ enter_pm:
113136 mov eax , cr0
114137 or eax , 0x1
115138 mov cr0 , eax
139+
140+ ; flush the prefetch input queue
141+ ; Perform far jump to selector 08h (offset into GDT, pointing at a 32bit PM code segment descriptor)
142+ ; to load CS with proper PM32 descriptor)
116143 jmp CODE_SEG:init_pm ; flushes real-mode commands
117144
118145[BITS 32]
@@ -127,7 +154,8 @@ init_pm:
127154 mov esp , ebp
128155 mov ebx , MSG_5
129156 call printpm
130- call KERNEL_ADDRESS
157+
158+ jmp KERNEL_ADDRESS
131159 jmp $
132160
133161
@@ -137,32 +165,63 @@ init_pm:
137165; string address in bx
138166[BITS 16]
139167println:
168+ call print
169+ mov al , 0x0D ; LF CR
170+ int 0x10
171+ mov al , 0x0A
172+ int 0x10
173+ ret
174+
175+ print:
140176 mov ah , 0x0e ; int 0x10 teletype output
141- .println_repe at :
177+ .print_repe at :
142178 mov al , [ bx ]
143179 cmp al , 0
144- je .println_done
180+ je .print_done
145181 int 0x10
146182 inc bx
147- jmp .println_repe at
148- .println_done:
149- mov al , 0x0D ; LF CR
150- int 0x10
151- mov al , 0x0A
152- int 0x10
183+ jmp .print_repe at
184+ .print_done:
153185 ret
154186
155187halt:
156188 mov bx , HALTING
157189 call println
158190 jmp $
159191
192+ ; int in bx, must be 000 - 999
193+ printint:
194+ push dx
195+ push bx
196+ push ax
197+ xor dx , dx
198+ mov ax , bx
199+ mov bx , 100
200+ div bx ;divide dx:ax by bx, store in ax, remainder in dx
201+ mov [ NUM_BUFFER ], al
202+ add [ NUM_BUFFER ], byte '0'
203+ mov ax , dx
204+ xor dx , dx
205+ mov bx , 10
206+ div bx
207+ mov [ NUM_BUFFER + 1 ], al
208+ add [ NUM_BUFFER + 1 ], byte '0'
209+ mov [ NUM_BUFFER + 2 ], dx
210+ add [ NUM_BUFFER + 2 ], byte '0'
211+ mov bx , NUM_BUFFER
212+ call print
213+ pop ax
214+ pop bx
215+ pop dx
216+ ret
217+
160218[BITS 32]
161219printpm:
162220 VIDEO_MEM equ 0xb8000
163221 WHITE_ON_BLACK equ 0x0f
164222 pusha
165223 mov edx , VIDEO_MEM
224+ add edx , 160 * 20
166225.repe at :
167226 mov al , [ ebx ]
168227 mov ah , WHITE_ON_BLACK
@@ -176,19 +235,20 @@ printpm:
176235 popa
177236 ret
178237
179-
180238[BITS 16]
181239
182240
183241BOOT_DRIVE db 0
184- MSG_1 db 'Bootloader is successfully running.' , 0
185- MSG_2 db 'Press a key to load and enter the kernel.' , 0
186- MSG_3 db 'Attempting to read the disk...' , 0
242+ MSG_KB db ' kb RAM' , 0
243+ MSG_2 db 'Press a key to load the kernel' , 0
244+ MSG_READ db 'Read ' , 0
245+ MSG_READING db 'Reading ' , 0
246+ MSG_FROMDISK db ' sectors from disk' , 0
187247MSG_4 db 'Done!' , 0
188- MSG_5 db 'Entering the kernel... ' , 0
189- DISK_ERROR db 'The disk read failed!' , 0
190- INCOMPLETE_READ db 'The wrong number of sectors were read! ' , 0
191- HALTING db 'Halting execution... ' , 0
248+ MSG_5 db 'PM: Entering kernel' , 0
249+ DISK_ERROR db 'Read failed!' , 0
250+ HALTING db 'Halting ' , 0
251+ NUM_BUFFER db '000 ' , 0
192252
193253times 510 - ($ - $$) db 0
194254dw 0xaa55
0 commit comments