Commit 523bff7f authored by Mathieu Maret's avatar Mathieu Maret

Put 32bits code after 1sector and load it

Use makefile to build
Use ld linker script to create final bin
parent a6750cb2
AS=nasm
ASFLAGS += -f elf32
LDFLAGS += -m32 -nostdlib -static -fno-common -fno-use-cxa-atexit -fno-exceptions -fno-non-call-exceptions -fno-weak -fno-rtti
CFLAGS += -m32 -Wall -Wextra -Werror -ffreestanding -fno-exceptions
CXXFLAGS += -m32 -Wall -Wextra -Werror -ffreestanding -fno-exceptions -fno-rtti
asmsrc=$(wildcard *.asm)
asmobj=$(asmsrc:%.asm=%.o)
csrc=$(wildcard *.c)
cobj=$(csrc:%.c=%.o)
kernel:$(asmobj) $(cobj) linker.ld
$(CXX) $(LDFLAGS) $(cobj) $(asmobj) -o $@ -T linker.ld
%.o:%.asm
$(AS) $(ASFLAGS) -o $@ $<
test:kernel
qemu-system-x86_64 -fda $<
clean:
$(RM) kernel $(asmobj) $(cobj)
# Instruction
Build with
`nasm -f bin mbr.asm -o mbr`
`make`
Run with
`qemu-system-x86_64 -boot a -fda mbr`
`make test`
Debug with
`qemu-system-x86_64 -boot a -fda mbr -s -S`
......
ENTRY(boot)
OUTPUT_FORMAT("binary")
SECTIONS {
. = 0x7c00;
.text :
{
*(.boot)
*(.text)
}
.rodata :
{
*(.rodata)
}
.data :
{
*(.data)
}
.bss :
{
*(.bss)
}
}
bits 16 ; mode 16bits
org 0x7C00 ; mbr ae loaded at 0x7C00
jmp boot
section .boot
bits 16
global boot
boot:
jmp main
display_enable:
push bp
......@@ -57,8 +58,9 @@ println:
hello db 'Hello world', 0
name db 'This is a name', 0
boot:
main:
sti ; enable virtual interupts
mov [disk],dl ; save disk used to boot by bios
call display_enable
......@@ -81,6 +83,15 @@ boot:
mov ax, 0x3
int 0x10
; Bios read first 512 bytes, read next disk sector
mov ah, 0x2 ;read sectors
mov al, 6 ;sectors to read
mov ch, 0 ;cylinder idx
mov dh, 0 ;head idx
mov cl, 2 ;sector idx
mov dl, [disk] ;disk idx
mov bx, copy_target;target pointer
int 0x13
cli ; clear interruption flag
......@@ -113,10 +124,14 @@ gdt_end:
gdt_pointer:
dw gdt_end - gdt_start
dd gdt_start
disk:
db 0x0
CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start
times 510 - ($-$$) db 0
dw 0xaa55
copy_target:
bits 32
boot2:
mov ax, DATA_SEG
......@@ -139,6 +154,4 @@ boot2:
halt:
cli
hlt
hello32: db "Hello world!",0
times 510 - ($ - $$) db 0 ; fill up to 510 with 0
dw 0xAA55 ; MBR magic number
hello32: db "Hello 32 bits world!",0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment