]> git.lizzy.rs Git - loadnothing.git/blob - stage1/boot.asm
2e995a0ace3c99056300eb5a9b9590d6c5023854
[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
17 mov bp, STAGE2START
18 mov sp, bp
19
20 jmp boot
21
22 ; Print al register
23 print_u8:
24         mov ah, 0x0e ; TTY output
25         mov bh, 0x00 ; Page 0
26         mov bl, 0x07 ; Color: Light grey on black background
27         int 0x10
28
29         ret
30
31 ; Call print_al on all characters in si
32 ; si must be null terminated
33 print_str:
34         mov cl, 0 ; Start with iteration 0 - equivalent of int i = 0
35 .print_str_loop:
36         lodsb              ; Load next characet of si into al
37
38         cmp al, 0          ; Null terminator?
39         je .print_str_exit ; If yes, we are done
40
41         call print_u8
42         inc cl
43
44         jmp .print_str_loop
45
46 .print_str_exit:
47         ret
48
49 stage2_error:
50         mov si, error
51         call print_str
52
53         jmp $               ; Infinite loop
54
55 ; Main
56 boot:
57         ; Clear the screen
58         mov ah, 0x06
59         mov al, 0x00
60         mov bh, 0x07
61         mov ch, 0x00
62         mov cl, 0x00
63         mov dh, 0xff
64         mov dl, 0xff
65         int 0x10
66
67         ; Move cursor to 0, 0 on page 0
68         mov ah, 0x02
69         mov bh, 0             ; Page
70         mov dh, 0             ; Row
71         mov dl, 0             ; Column
72         int 0x10
73
74         mov si, hello
75         call print_str
76
77         mov ah, 0x02          ; Read sectors
78         mov al, STAGE2SECTORS ; Stage 2 size 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, STAGE2SECTORS ; 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, 0 ; \r\n\0
94 error db 'Error reading stage 2 from disk', 13, 10, 0 ; \r\n\0
95
96 times (446 - ($ - $$)) db 0x00