OS Programming - boot.asm

08 Aug 2025

; boot.asm
;
; Copyright (c) 2025 fbmnn
;
; Permission to use, copy, modify, and distribute this software for any
; purpose with or without fee is hereby granted, provided that the above
; copyright notice and this permission notice appear in all copies.
;
; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

[org 0x7c00]
[bits 16]

start:
    jmp begin
    nop
    db "        "    ; oem id
    dw 512           ; sector size
    db 0             ; sectors per cluster
    dw 0             ; reserved sectors
    db 0             ; fat count
    dw 0             ; root dir entries
    dw 0             ; sector count
    db 0             ; media type
    dw 0             ; sectors per fat
    dw 18            ; sectors per track
    dw 2             ; heads count
    dd 0             ; hidden sectors
    dd 0             ; sector count big
    db 0             ; drive number
    db 0             ; reserved / flags
    db 0             ; signature
    dd 0             ; volume id
    db "           " ; volume label
    times 8 db 0     ; filesystem type

begin:
    jmp 0x0000:flushcs

flushcs:
    xor ax, ax
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov sp, 0x7c00

    call load_kernel

%include "tools/print_string.asm"
%include "tools/disk_load.asm"
%include "tools/gdt.asm"
%include "tools/print_string_pm.asm"
%include "tools/switch_to_pm.asm"

[bits 16]
load_kernel:
    mov bx, MSG_LOAD_KERNEL
    call print_string

    mov bx, KERNEL_OFFSET
    mov dh, 15
    call disk_load
    call switch_to_pm

[bits 32]
BEGIN_PM:
    call KERNEL_OFFSET

MSG_LOAD_KERNEL db "Loading kernel...", 0

times 510-($-$$) db 0
dw 0xaa55

KERNEL_OFFSET:
incbin "kernel/kernel.bin"