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