Skip to content

Commit 418bb11

Browse files
author
Carlos Fenollosa
committed
Lesson 18
1 parent 69a7c1c commit 418bb11

File tree

17 files changed

+412
-16
lines changed

17 files changed

+412
-16
lines changed

14-checkpoint/boot/32bit_print.asm

Lines changed: 0 additions & 1 deletion
This file was deleted.

14-checkpoint/boot/32bit_print.asm

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[bits 32] ; using 32-bit protected mode
2+
3+
; this is how constants are defined
4+
VIDEO_MEMORY equ 0xb8000
5+
WHITE_OB_BLACK equ 0x0f ; the color byte for each character
6+
7+
print_string_pm:
8+
pusha
9+
mov edx, VIDEO_MEMORY
10+
11+
print_string_pm_loop:
12+
mov al, [ebx] ; [ebx] is the address of our character
13+
mov ah, WHITE_OB_BLACK
14+
15+
cmp al, 0 ; check if end of string
16+
je print_string_pm_done
17+
18+
mov [edx], ax ; store character + attribute in video memory
19+
add ebx, 1 ; next char
20+
add edx, 2 ; next video memory position
21+
22+
jmp print_string_pm_loop
23+
24+
print_string_pm_done:
25+
popa
26+
ret

14-checkpoint/boot/disk.asm

Lines changed: 0 additions & 1 deletion
This file was deleted.

14-checkpoint/boot/disk.asm

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
; load 'dh' sectors from drive 'dl' into ES:BX
2+
disk_load:
3+
pusha
4+
; reading from disk requires setting specific values in all registers
5+
; so we will overwrite our input parameters from 'dx'. Let's save it
6+
; to the stack for later use.
7+
push dx
8+
9+
mov ah, 0x02 ; ah <- int 0x13 function. 0x02 = 'read'
10+
mov al, dh ; al <- number of sectors to read (0x01 .. 0x80)
11+
mov cl, 0x02 ; cl <- sector (0x01 .. 0x11)
12+
; 0x01 is our boot sector, 0x02 is the first 'available' sector
13+
mov ch, 0x00 ; ch <- cylinder (0x0 .. 0x3FF, upper 2 bits in 'cl')
14+
; dl <- drive number. Our caller sets it as a parameter and gets it from BIOS
15+
; (0 = floppy, 1 = floppy2, 0x80 = hdd, 0x81 = hdd2)
16+
mov dh, 0x00 ; dh <- head number (0x0 .. 0xF)
17+
18+
; [es:bx] <- pointer to buffer where the data will be stored
19+
; caller sets it up for us, and it is actually the standard location for int 13h
20+
int 0x13 ; BIOS interrupt
21+
jc disk_error ; if error (stored in the carry bit)
22+
23+
pop dx
24+
cmp al, dh ; BIOS also sets 'al' to the # of sectors read. Compare it.
25+
jne sectors_error
26+
popa
27+
ret
28+
29+
30+
disk_error:
31+
mov bx, DISK_ERROR
32+
call print
33+
call print_nl
34+
mov dh, ah ; ah = error code, dl = disk drive that dropped the error
35+
call print_hex ; check out the code at http://stanislavs.org/helppc/int_13-1.html
36+
jmp disk_loop
37+
38+
sectors_error:
39+
mov bx, SECTORS_ERROR
40+
call print
41+
42+
disk_loop:
43+
jmp $
44+
45+
DISK_ERROR: db "Disk read error", 0
46+
SECTORS_ERROR: db "Incorrect number of sectors read", 0

14-checkpoint/boot/gdt.asm

Lines changed: 0 additions & 1 deletion
This file was deleted.

14-checkpoint/boot/gdt.asm

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
gdt_start: ; don't remove the labels, they're needed to compute sizes and jumps
2+
; the GDT starts with a null 8-byte
3+
dd 0x0 ; 4 byte
4+
dd 0x0 ; 4 byte
5+
6+
; GDT for code segment. base = 0x00000000, length = 0xfffff
7+
; for flags, refer to os-dev.pdf document, page 36
8+
gdt_code:
9+
dw 0xffff ; segment length, bits 0-15
10+
dw 0x0 ; segment base, bits 0-15
11+
db 0x0 ; segment base, bits 16-23
12+
db 10011010b ; flags (8 bits)
13+
db 11001111b ; flags (4 bits) + segment length, bits 16-19
14+
db 0x0 ; segment base, bits 24-31
15+
16+
; GDT for data segment. base and length identical to code segment
17+
; some flags changed, again, refer to os-dev.pdf
18+
gdt_data:
19+
dw 0xffff
20+
dw 0x0
21+
db 0x0
22+
db 10010010b
23+
db 11001111b
24+
db 0x0
25+
26+
gdt_end:
27+
28+
; GDT descriptor
29+
gdt_descriptor:
30+
dw gdt_end - gdt_start - 1 ; size (16 bit), always one less of its true size
31+
dd gdt_start ; address (32 bit)
32+
33+
; define some constants for later use
34+
CODE_SEG equ gdt_code - gdt_start
35+
DATA_SEG equ gdt_data - gdt_start

14-checkpoint/boot/kernel_entry.asm

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[bits 32]
2+
[extern main] ; Define calling point. Must have same name as kernel.c 'main' function
3+
call main ; Calls the C function. The linker will know where it is placed in memory
4+
jmp $

14-checkpoint/boot/print.asm

Lines changed: 0 additions & 1 deletion
This file was deleted.

14-checkpoint/boot/print.asm

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
print:
2+
pusha
3+
4+
; keep this in mind:
5+
; while (string[i] != 0) { print string[i]; i++ }
6+
7+
; the comparison for string end (null byte)
8+
start:
9+
mov al, [bx] ; 'bx' is the base address for the string
10+
cmp al, 0
11+
je done
12+
13+
; the part where we print with the BIOS help
14+
mov ah, 0x0e
15+
int 0x10 ; 'al' already contains the char
16+
17+
; increment pointer and do next loop
18+
add bx, 1
19+
jmp start
20+
21+
done:
22+
popa
23+
ret
24+
25+
26+
27+
print_nl:
28+
pusha
29+
30+
mov ah, 0x0e
31+
mov al, 0x0a ; newline char
32+
int 0x10
33+
mov al, 0x0d ; carriage return
34+
int 0x10
35+
36+
popa
37+
ret

0 commit comments

Comments
 (0)