diff --git a/worksheet2/Makefile b/worksheet2/Makefile index 17421642cba7df7fbf014372dc0956cf25c92aa9..89047dc870806c4b46d647ee5e3013625b5c4a5c 100644 --- a/worksheet2/Makefile +++ b/worksheet2/Makefile @@ -1,26 +1,24 @@ -# Makefile for building and running the OS kernel - -# Directories dirs = drivers source iso iso/boot iso/boot/grub -# Files kernel = iso/boot/kernel.elf iso_image = os.iso loader = source/loader.asm loader_obj = source/loader.o +kernel_c = source/kernel.c +kernel_obj = source/kernel.o linker_script = source/link.ld menu_lst = iso/boot/grub/menu.lst stage2_file = /opt/os/stage2_eltorito log_file = logQ.txt -# Compiler and tools NASM = nasm +CC = gcc LD = ld QEMU = qemu-system-i386 GENISOIMAGE = genisoimage -# Flags NASM_FLAGS = -f elf +CFLAGS = -m32 -ffreestanding -nostdlib -nodefaultlibs LD_FLAGS = -T $(linker_script) -melf_i386 GENISO_FLAGS = -R \ -b boot/grub/stage2_eltorito \ @@ -32,24 +30,28 @@ GENISO_FLAGS = -R \ -boot-info-table QEMU_FLAGS = -nographic -boot d -cdrom $(iso_image) -m 32 -d cpu -D $(log_file) -# Targets -.PHONY: all clean run +.PHONY: all clean run task1 task2 + +all: task2 -all: $(dirs) $(iso_image) +task1: NASM_FLAGS += -dTASK1 +task1: $(dirs) $(iso_image) + +task2: NASM_FLAGS += -dTASK2 +task2: $(dirs) $(iso_image) -# Create required directories $(dirs): mkdir -p $@ -# Build loader object file $(loader_obj): $(loader) $(NASM) $(NASM_FLAGS) -o $@ $< -# Build the kernel -$(kernel): $(loader_obj) $(linker_script) - $(LD) $(LD_FLAGS) $(loader_obj) -o $@ +$(kernel_obj): $(kernel_c) + $(CC) $(CFLAGS) -c $< -o $@ + +$(kernel): $(loader_obj) $(linker_script) $(kernel_obj) + $(LD) $(LD_FLAGS) $(loader_obj) $(kernel_obj) -o $@ -# Copy files to GRUB directory iso/boot/grub/stage2_eltorito: cp $(stage2_file) $@ @@ -59,16 +61,13 @@ $(menu_lst): echo "title os" >> $@ echo "kernel /boot/kernel.elf" >> $@ -# Build the ISO image $(iso_image): iso/boot/grub/stage2_eltorito $(menu_lst) $(kernel) $(GENISOIMAGE) $(GENISO_FLAGS) -o $@ iso -# Run the OS in QEMU run: $(iso_image) $(QEMU) $(QEMU_FLAGS) -# Clean up generated files clean: - rm -rf source/loader.o $(iso_image) $(log_file) + rm -rf source/loader.o source/kernel.o $(iso_image) $(log_file) rm -rf iso/boot/kernel.elf iso/boot/grub/stage2_eltorito $(menu_lst) rmdir --ignore-fail-on-non-empty $(dirs) diff --git a/worksheet2/source/kernel.c b/worksheet2/source/kernel.c index 49fb14f2dfb3c782b6eef149a3434580ed90537a..5632920c8b9c36dc233d998079465d1bf4dcee05 100644 --- a/worksheet2/source/kernel.c +++ b/worksheet2/source/kernel.c @@ -1,11 +1,17 @@ + #include <stddef.h> + int sum_of_three(int a, int b, int c) { return a + b + c; } -int multiply(int a, int b) { +int multiply_two(int a, int b) { return a * b; } -int subtract(int a, int b) { - return a - b; +void c_entry_point() { + volatile int result_sum = sum_of_three(1, 2, 3); + + volatile int result_product = multiply_two(4, 5); + + while (1); } diff --git a/worksheet2/source/link.ld b/worksheet2/source/link.ld index ed14d2a7df5d30c4468f43ef67e4ea68084bc5a7..f177851f24a56278562c6014254e18bc691b9425 100644 --- a/worksheet2/source/link.ld +++ b/worksheet2/source/link.ld @@ -1,9 +1,21 @@ -ENTRY(loader) - +ENTRY(loader) SECTIONS { - . = 0x00100000; - .text ALIGN (0x1000) : { *(.text) } - .rodata ALIGN (0x1000) : { *(.rodata*) } - .data ALIGN (0x1000) : { *(.data) } - .bss ALIGN (0x1000) : { *(COMMON) *(.bss) } + . = 0x00100000; + + .text ALIGN(0x1000) : { + *(.text) + } + + .rodata ALIGN(0x1000) : { + *(.rodata*) + } + + .data ALIGN(0x1000) : { + *(.data) + } + + .bss ALIGN(0x1000) : { + *(COMMON) + *(.bss) + } } diff --git a/worksheet2/source/loader.asm b/worksheet2/source/loader.asm index 0a7d3c37003169e7296c7806d1b21e1d89d9c174..0a53dadbdae9d2d1042a63240ab2ec233135baae 100644 --- a/worksheet2/source/loader.asm +++ b/worksheet2/source/loader.asm @@ -1,18 +1,15 @@ -global loader ; The entry symbol for the ELF file - -; Define constants for multiboot -MAGIC_NUMBER equ 0x1BADB002 ; Multiboot magic number -FLAGS equ 0x0 ; Multiboot flags -CHECKSUM equ -(MAGIC_NUMBER) ; Checksum (magic number + flags + checksum = 0) +global loader ; +MAGIC_NUMBER equ 0x1BADB002 ; +FLAGS equ 0x0 ; +CHECKSUM equ -(MAGIC_NUMBER + FLAGS) ; section .text -align 4 ; Align to 4 bytes -dd MAGIC_NUMBER ; Write the magic number -dd FLAGS ; Write the flags -dd CHECKSUM ; Write the checksum +align 4 +dd MAGIC_NUMBER ; +dd FLAGS ; +dd CHECKSUM ; -loader: ; Entry point (defined in link.ld) - mov eax, 0xCAFEBABE ; Place the value in EAX +loader: + mov eax, 0xCAFEBABE ; .loop: - jmp .loop ; Infinite loop - + jmp .loop ; Infinite loop