aboutsummaryrefslogtreecommitdiffstats
path: root/src/boot/boot.stage1.asm
blob: 5fa3b7a6c2a3163493ace3729b8bcef5fea2143a (plain)
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
    ;; boot.stage1.asm

    [org 0x7c00]    
    [bits 16]

    %include "boot.common.asm"

    %define READ_SECTORS_NUM 1
    %define BOOT_LOAD_ADDR 0x7c00
    %define SECTOR_SIZE 512

    global _start

_start: 
    ;; Disable interrupts
    cli                         

    ;; Setup segment registers
    xor ax, ax
    mov ds, ax
    mov es, ax  
    mov ss, ax

    ;; Adjust Stack
    mov sp, _start
    
    clc

    mov [drive], dl
    mov si, disk_addr_packet
    mov ah, 0x42                ; BIOS extended read function
    mov dl, [drive]             ; Drive number
    int 0x13                    ; BIOS disk services
    jc _disk_read_err
    
    mov si, disk_read_success_msg
    call _print_string

    jmp 0x0000:0x7e00

_disk_read_err: 
    mov si, disk_read_err_msg
    call _print_string

    hlt
    jmp _disk_read_err

_print_string:  
    pusha
    mov ah, 0x0e
    mov bh, 0x00

.print_loop:     
    lodsb
    test al, al
	je .print_return
    int 0x10
	jmp .print_loop

.print_return:
	popa
	ret

_end:
	hlt 
	jmp _end

drive:   db 0
disk_read_err_msg: db "Failed to read disk", 13, 10, 0
disk_read_success_msg: db "Read the disk successfully", 13, 10, 0
disk_addr_packet:  
	db 0x10
	db 0x00
	dw READ_SECTORS_NUM
	dw 0x7e00
	dw 0x0000
	dq 1

	;; Padding and magic number
	times 510 - ($ - $$) db 0
	dw 0xaa55