Browse Source

Put 32bits code after 1sector and load it

Use makefile to build
Use ld linker script to create final bin
master
Mathieu Maret 4 years ago
parent
commit
523bff7fce
4 changed files with 73 additions and 11 deletions
  1. +23
    -0
      Makefile
  2. +2
    -2
      Readme.md
  3. +26
    -0
      linker.ld
  4. +22
    -9
      mbr.asm

+ 23
- 0
Makefile View 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)

+ 2
- 2
Readme.md View File

@ -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
- 0
linker.ld View File

@ -0,0 +1,26 @@
ENTRY(boot)
OUTPUT_FORMAT("binary")
SECTIONS {
. = 0x7c00;
.text :
{
*(.boot)
*(.text)
}
.rodata :
{
*(.rodata)
}
.data :
{
*(.data)
}
.bss :
{
*(.bss)
}
}

+ 22
- 9
mbr.asm View File

@ -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…
Cancel
Save