Put 32bits code after 1sector and load it
Use makefile to build Use ld linker script to create final bin
This commit is contained in:
parent
a6750cb2a5
commit
523bff7fce
23
Makefile
Normal file
23
Makefile
Normal file
@ -0,0 +1,23 @@
|
||||
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)
|
@ -1,10 +1,10 @@
|
||||
# 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`
|
||||
|
26
linker.ld
Normal file
26
linker.ld
Normal file
@ -0,0 +1,26 @@
|
||||
ENTRY(boot)
|
||||
OUTPUT_FORMAT("binary")
|
||||
SECTIONS {
|
||||
. = 0x7c00;
|
||||
.text :
|
||||
{
|
||||
*(.boot)
|
||||
*(.text)
|
||||
}
|
||||
|
||||
.rodata :
|
||||
{
|
||||
*(.rodata)
|
||||
}
|
||||
|
||||
.data :
|
||||
{
|
||||
*(.data)
|
||||
}
|
||||
|
||||
.bss :
|
||||
{
|
||||
*(.bss)
|
||||
}
|
||||
}
|
||||
|
31
mbr.asm
31
mbr.asm
@ -1,7 +1,8 @@
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user