]> git.lizzy.rs Git - loadnothing.git/blob - stage1/boot.asm
Load stage2 from boot drive rather than fixed hard disk
[loadnothing.git] / stage1 / boot.asm
1 [org 0x7c00]
2
3 %define STAGE2START 0x7e00
4 %define STAGE2SECTORS (STAGE2SIZE + 511) / 512
5
6 ; Initialize registers
7 xor ax, ax
8 mov ds, ax
9 mov es, ax
10 mov fs, ax
11 mov gs, ax
12 mov ss, ax
13
14 ; Initialize the stack
15 ; It grows down, overwriting this code
16 ; I have no idea what this does exactly (note by Lizzy: bp is base pointer, sp is stack pointer)
17 mov bp, STAGE2START
18 mov sp, bp
19
20 push dx ; Save boot drive (will be restored when making the int 0x13 ah=0x02 call)
21
22 jmp boot
23
24 ; Print al register
25 print_u8:
26         mov ah, 0x0e ; TTY output
27         mov bh, 0x00 ; Page 0
28         mov bl, 0x07 ; Color: Light grey on black background
29         int 0x10
30
31         ret
32
33 ; Call print_al on all characters in si
34 ; si must be null terminated
35 print_str:
36         mov cl, 0 ; Start with iteration 0 - equivalent of int i = 0
37 .print_str_loop:
38         lodsb              ; Load next characet of si into al
39
40         cmp al, 0          ; Null terminator?
41         je .print_str_exit ; If yes, we are done
42
43         call print_u8
44         inc cl
45
46         jmp .print_str_loop
47
48 .print_str_exit:
49         ret
50
51 stage2_error:
52         mov si, error
53         call print_str
54
55         jmp $               ; Infinite loop
56
57 ; Main
58 boot:
59         ; Clear the screen
60         mov ah, 0x06
61         mov al, 0x00
62         mov bh, 0x07
63         mov ch, 0x00
64         mov cl, 0x00
65         mov dh, 0xff
66         mov dl, 0xff
67         int 0x10
68
69         ; Move cursor to 0, 0 on page 0
70         mov ah, 0x02
71         mov bh, 0             ; Page
72         mov dh, 0             ; Row
73         mov dl, 0             ; Column
74         int 0x10
75
76         mov si, hello
77         call print_str
78
79         mov ah, 0x02          ; Read sectors
80         mov al, STAGE2SECTORS ; Stage 2 size in sectors
81         xor ch, ch            ; Cylinder 0
82         mov cl, 2             ; Second sector, they start at 1
83         pop dx                ; Restore boot drive
84         xor dh, dh            ; Head 0
85         mov bx, STAGE2START   ; Memory address to load stage 2 into
86         int 0x13
87
88         jc stage2_error       ; Carry flag is set if there was an error
89
90         cmp al, STAGE2SECTORS ; Have we read as many sectors as we requested?
91         jne stage2_error
92
93         jmp STAGE2START       ; Hand over control to stage 2
94
95 hello db 'Welcome to loadnothing stage 1!', 13, 10, 0 ; \r\n\0
96 error db 'Error reading stage 2 from disk', 13, 10, 0 ; \r\n\0
97
98 times (446 - ($ - $$)) db 0x00