Skip to content

Commit 7e9e764

Browse files
committed
Add the Reading from disk section
1 parent 3c2ff8b commit 7e9e764

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

doc/1_Real_mode.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,18 @@ The values of 0x08-0x11 are 00 01 11 11, which means the address of ISR (int 0x0
6767
Physical address = (0x1111 * 16) + 0x0001 = 0x11110 + 0x0001 = 0x11111
6868
```
6969

70-
To define a custom interrupt handler, we first define a routine with a label and then write the segment/offset of the routine to IVT.
70+
To define a custom interrupt handler, we first define a routine with a label and then write the segment/offset of the routine to IVT.f
71+
72+
## Reading From The Disk
73+
74+
The bootloader is appended the content of [boot5.txt](../examples/bootloader/boot5.txt) at the end of binary file (see [Makefile](../examples/bootloader/Makefile)).
75+
76+
When we start up a QEMU with `boot5.bin`, it treats it as a hard disk.
77+
78+
Whenever the CPU reads data from a hard disk, it must be one full sector (512 bytes). We need to make sure that our message (starting from the second sector 0x200 because we use the first sector for our bootloader code), is padded with zeros until the end of the sector.
79+
80+
Disk access is done via [`Int13h/AH=02h`](http://www.ctyme.com/intr/rb-0607.htm).
81+
82+
Note how we created an empty label called `buffer` at the very end of the bootloader code. Because the bootloader code is 512 bytes, this label is not loaded into the memory. That doesn't mean we cannot use the memory pointed by this label. Since the label is at the end, it points to 0x7e00 = 0x7c00 (start of the bootloader) + 0x200 (512 bytes).
83+
84+
![RESULT](./img/1_Real_mode_final.png)

doc/img/1_Real_mode_final.png

23.7 KB
Loading

examples/bootloader/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
all:
2+
rm -f boot5.bin
3+
nasm -f bin -o boot5.bin boot5.asm
4+
dd if=./boot5.txt >> boot5.bin
5+
dd if=/dev/zero bs=512 count=1 >> boot5.bin

examples/bootloader/boot5.asm

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
ORG 0
2+
BITS 16 ; 16-bit code, ensure the assembler only assemble 16-bit code
3+
4+
_start:
5+
jmp short start
6+
nop
7+
8+
times 33 db 0 ; Fill the rest of sector with 0
9+
10+
start: ; Entry point
11+
jmp 0x07C0:second_stage
12+
13+
second_stage:
14+
cli ; Clear interrupts
15+
16+
mov ax, 0x07C0 ; Load boot sector address to AX register
17+
mov ds, ax ; Load data segment to DS register
18+
mov es, ax ; Load data segment to ES register
19+
20+
mov ax, 0x0000
21+
mov ss, ax ; Load stack segment to SS register
22+
mov sp, 0x07C0 ; Load stack pointer to SP register
23+
24+
sti ; Set interrupts
25+
26+
mov ah, 0x02 ; Read disk command
27+
mov al, 0x01 ; Number of sectors to read
28+
mov ch, 0x00 ; Cylinder number
29+
mov cl, 0x02 ; Sector number
30+
mov dh, 0x00 ; Head number
31+
mov bx, buffer ; Buffer address
32+
int 0x13 ; Call BIOS disk interrupt
33+
34+
jc error ; If carry flag is set, jump to error
35+
36+
mov si, buffer ; Load buffer address to SI register
37+
call print ; Call print function
38+
39+
jmp $ ; Infinite loop
40+
41+
error:
42+
mov si, error_message ; Load error message address to SI register
43+
call print ; Call print function
44+
ret
45+
46+
print: ; Print function
47+
mov bx, 0 ; Page number
48+
.loop:
49+
lodsb ; Load byte from DS:SI to AL and increment SI
50+
cmp al, 0 ; Check if AL is null
51+
je .done ; If AL is null, jump to done
52+
mov ah, 0x0E ; BIOS teletype function
53+
int 0x10 ; Call BIOS (http://www.ctyme.com/intr/rb-0106.htm)
54+
jmp .loop ; Loop
55+
.done:
56+
ret ; Return
57+
58+
error_message: db 'Error reading disk', 0 ; Null-terminated string
59+
60+
times 510-($-$$) db 0 ; Fill the rest of sector with 0
61+
dw 0xAA55 ; Boot signature
62+
63+
buffer:

examples/bootloader/boot5.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world, this is my awesome message!

0 commit comments

Comments
 (0)