-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootloader.asm
More file actions
68 lines (56 loc) · 2.88 KB
/
bootloader.asm
File metadata and controls
68 lines (56 loc) · 2.88 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
; bootloader.asm
; A simple 16-bit bootloader that loads a kernel from the next sector.
org 0x7C00 ; BIOS loads boot sector at this address
bits 16 ; We are in 16-bit real mode
; --- Initialize segment registers ---
; The BIOS initializes CS:IP to 0x07C0:0x0000.
; Set DS, ES, SS to 0x07C0 as well for consistent addressing of bootloader data.
; Set SP to 0x7C00 (or slightly before) so the stack grows downwards from within our bootloader's segment.
xor ax, ax ; AX = 0
mov ds, ax ; DS = 0 (for accessing global data if needed)
mov es, ax ; ES = 0 (for extra segment access)
mov ss, ax ; SS = 0 (stack segment)
mov sp, 0x7C00 ; SP points to the end of our 512-byte boot sector,
; so the stack grows downwards into memory *before* the boot sector.
; --- Print "Loading LuckyOS Kernel..." message ---
mov si, msg_loading ; Load address of message into SI
call print_string ; Call our string printing routine
; --- Load Kernel ---
; INT 13h, AH=0x02: Read Sectors From Drive
mov ah, 0x02 ; Function: Read Sectors
mov al, 20 ; Number of sectors to read (Increased to 20 for your kernel)
mov ch, 0 ; Cylinder (track) number = 0
mov cl, 2 ; Starting sector number = 2 (sector 1 is 0-indexed, so 2nd physical sector)
; Bootloader is sector 1 (0-indexed). Kernel starts right after.
mov dh, 0 ; Head number = 0
mov dl, 0x00 ; Drive number (0x00 for floppy A:, 0x80 for first hard drive)
; Changed to 0x00 for common floppy drive usage with -fda
mov bx, 0x1000 ; Offset into ES (which is currently 0) for target address
; Kernel will be loaded to 0x0000:0x1000 (linear address 0x1000)
int 0x13 ; Call BIOS disk service
jc disk_error ; If carry flag set (error), jump to error handler
; --- Jump to Kernel ---
; Jump to the loaded kernel's entry point
jmp 0x0000:0x1000 ; Far jump to segment 0x0000, offset 0x1000
; --- String Printing Routine (used by bootloader) ---
print_string:
mov ah, 0x0E ; Teletype output (BIOS INT 10h function)
.repeat:
lodsb ; Load byte from [DS:SI] into AL, increment SI
cmp al, 0 ; Check for null terminator
je .done
int 0x10 ; Print character
jmp .repeat
.done:
ret
; --- Error Handler ---
disk_error:
mov si, msg_error ; Load address of error message
call print_string
jmp $ ; Infinite loop to halt
; --- Data ---
msg_loading db 'Loading LuckyOS Kernel...', 0
msg_error db 'Disk Error! Failed to load the kernel.', 0
; --- Boot Sector Padding and Signature ---
times 510 - ($ - $$) db 0 ; Pad with zeros to 510 bytes
dw 0xAA55 ; Boot signature (0x55AA)