diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e2af9ab --- /dev/null +++ b/Makefile @@ -0,0 +1,46 @@ +ASM := nasm +CC := gcc +LD := ld +OBJCOPY := objcopy +QEMU := qemu-system-x86_64 + +BOOT_DIR := boot +KERNEL_DIR := kernel +OUT_DIR := out + +BOOT_BIN := $(OUT_DIR)/bootloader.bin +KERNEL_ELF := $(OUT_DIR)/kernel.elf +KERNEL_BIN := $(OUT_DIR)/kernel.bin +OS_IMG := $(OUT_DIR)/os.img + +ASMFLAGS := -f bin +CFLAGS := -ffreestanding -nostdlib -m32 -Wall -Wextra -O2 -std=c11 +LDFLAGS := -m elf_i386 -nostdlib -Ttext 0x1000 + +.PHONY: all build run clean + +all: build + +build: $(OS_IMG) + +$(OUT_DIR): + mkdir -p $@ + +$(BOOT_BIN): $(BOOT_DIR)/bootloader.asm | $(OUT_DIR) + $(ASM) $(ASMFLAGS) -o $@ $< + +$(KERNEL_ELF): $(KERNEL_DIR)/kernel.c | $(OUT_DIR) + $(CC) $(CFLAGS) -nostartfiles -Wl,-e,kernel_main -o $@ $< + +$(KERNEL_BIN): $(KERNEL_ELF) | $(OUT_DIR) + $(OBJCOPY) -O binary $< $@ + +$(OS_IMG): $(BOOT_BIN) $(KERNEL_BIN) | $(OUT_DIR) + cat $(BOOT_BIN) $(KERNEL_BIN) > $@ + dd if=/dev/zero bs=1024 count=1440 >> $@ 2>/dev/null || true + +run: $(OS_IMG) + $(QEMU) -fda $< + +clean: + rm -rf $(OUT_DIR) diff --git a/boot/bootloader.asm b/boot/bootloader.asm new file mode 100644 index 0000000..798db34 --- /dev/null +++ b/boot/bootloader.asm @@ -0,0 +1,22 @@ +mov ah, 0x0e +mov al, 65 ; starting with capital A +int 0x10 + +; print the alphabet while alternating caps +.loop: + cmp al, 120 ; 122 is for z + jg .done + + xor al, 32 ; the space for each capital/lowercase is 32 + inc al + + int 0x10 + jmp .loop + + +.done: + jmp $ ; jump to current address forever + +; Boot sector padding +times 510 - ($ - $$) db 0x0 +dw 0xaa55 \ No newline at end of file