wrote a basic bootloader

This commit is contained in:
2025-02-25 01:14:52 +00:00
commit 012b0454ed
36 changed files with 1667 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
ENTRY(_start)
OUTPUT_FORMAT(binary)
SECTIONS
{
. = 0x7C00; /* Where BIOS loads us */
.text : {
*(.text)
. = 446; /* Pad to 510 bytes */
}
}
+92
View File
@@ -0,0 +1,92 @@
section .text
global _start
[BITS 16]
_start:
; Set video mode (clear screen)
mov ah, 0x00
mov al, 0x03
int 0x10
; Print loading message
mov ax, 0xb800
mov ds, ax
mov dword [ 0], "L O "
mov dword [ 4], "A D "
mov dword [ 8], "I N "
mov dword [ 12], "G . "
mov dword [ 16], ". . "
; setup a stack
cli
mov ax, 0x9000
mov ss, ax
mov sp, 0x0000
mov bp, sp
mov bx, 0x0800 ; this is the first memory location we're interested in.
mov es, bx
xor bx, bx
mov cl, 0x02 ; this is the first sector we're interested in.
call load_sector
mov ax, [es:6] ; load last two bytes (0x1FE = 510)
cmp ax, 0xAA55 ; check if it matches boot signature
jne error ; if not equal, not a valid boot sector
; jmp success
mov ax, [es:4] ; load sector count
next_sector:
dec ax ; decrement sectors left to write
inc cl
cmp ax, 0
je success
add bx, 0x200 ; Add 512 to offset
call load_sector
jmp next_sector
load_sector:
pusha
; setup registers for disk read
mov ah, 0x02 ; read sectors function
mov dl, 0x80 ; first hard disk
mov ch, 0x00 ; cylinder 0
mov dh, 0x00 ; head 0
mov al, 1 ; read 1 sector
; do the read
int 0x13
jc error ; if carry set, read failed
popa
ret
hang:
jmp hang
error:
; error getting the magic number
mov ah, 0x00
mov al, 0x03
int 0x10
mov ax, 0xb800
mov ds, ax
mov dword [ 0], "E R "
mov dword [ 4], "R O "
mov dword [ 8], "R . "
jmp hang
success:
jmp 0x0800:0x0016 ; jump to start of stage 2