diff --git a/Worksheet2/makefile b/Worksheet2/makefile index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..5b57ad4345e11ccbecc31afa7058b7736b9e8acc 100644 --- a/Worksheet2/makefile +++ b/Worksheet2/makefile @@ -0,0 +1,37 @@ +OBJECTS = loader.o kmain.o + CC = gcc + CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector \ + -nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c + LDFLAGS = -T link.ld -melf_i386 + AS = nasm + ASFLAGS = -f elf + + all: kernel.elf + + kernel.elf: $(OBJECTS) + ld $(LDFLAGS) $(OBJECTS) -o kernel.elf + + os.iso: kernel.elf + cp kernel.elf iso/boot/kernel.elf + genisoimage -R \ + -b boot/grub/stage2_eltorito \ + -no-emul-boot \ + -boot-load-size 4 \ + -A os \ + -input-charset utf8 \ + -quiet \ + -boot-info-table \ + -o os.iso \ + iso + + run: os.iso + qemu-system-i386 -nographic -boot d -cdrom os.iso -m 32 -d cpu -D logQ.txt + + %.o: %.c + $(CC) $(CFLAGS) $< -o $@ + + %.o: %.s + $(AS) $(ASFLAGS) $< -o $@ + + clean: + rm -rf *.o kernel.elf os.iso diff --git a/Worksheet2/source/iso/boot/grub/menu.lst b/Worksheet2/source/iso/boot/grub/menu.lst index 5ee35ea22659704ed7d10bcee3d4f148e2627860..29b4ac4ac87bcbc6bed96f865a977333f158673c 100644 --- a/Worksheet2/source/iso/boot/grub/menu.lst +++ b/Worksheet2/source/iso/boot/grub/menu.lst @@ -1,5 +1,5 @@ - default=0 - timeout=0 +default=0 +timeout=0 - title os - kernel /boot/kernel.elf \ No newline at end of file +title My OS +kernel /boot/kernel.elf diff --git a/Worksheet2/source/iso/boot/kernel.elf b/Worksheet2/source/iso/boot/kernel.elf index 010dd73cde1155314f9d9901e86f53b5df6909a4..44cfc49df2bb024c4317eeffed55276fbf41c279 100755 Binary files a/Worksheet2/source/iso/boot/kernel.elf and b/Worksheet2/source/iso/boot/kernel.elf differ diff --git a/Worksheet2/source/kernel.elf b/Worksheet2/source/kernel.elf index 010dd73cde1155314f9d9901e86f53b5df6909a4..38b2f1cc4bf6126c5cb654cb55afc075512ac08a 100755 Binary files a/Worksheet2/source/kernel.elf and b/Worksheet2/source/kernel.elf differ diff --git a/Worksheet2/source/link.ld b/Worksheet2/source/link.ld index 4600a2841556a51183af44454cbd59e8360eb8b1..62bb312508a6d4fc041d3e02a3ffbb2eb1e16894 100644 --- a/Worksheet2/source/link.ld +++ b/Worksheet2/source/link.ld @@ -1,26 +1,22 @@ -ENTRY(loader) /* the name of the entry label */ +ENTRY(loader) /* Specify the entry point symbol for the linker */ SECTIONS { - . = 0x00100000; /* the code should be loaded at 1 MB */ + . = 0x00100000; /* The code is to be loaded at 1 MB */ - .text ALIGN (0x1000) : /* align at 4 KB */ - { - *(.text) /* all text sections from all files */ - } + .text ALIGN(0x1000) : { /* Align the .text section to a 4 KB boundary */ + *(.text) /* Include all text sections from all files */ + } - .rodata ALIGN (0x1000) : /* align at 4 KB */ - { - *(.rodata*) /* all read-only data sections from all files */ - } + .rodata ALIGN(0x1000) : { /* Align the .rodata section to a 4 KB boundary */ + *(.rodata*) /* Include all read-only data sections from all files */ + } - .data ALIGN (0x1000) : /* align at 4 KB */ - { - *(.data) /* all data sections from all files */ - } + .data ALIGN(0x1000) : { /* Align the .data section to a 4 KB boundary */ + *(.data) /* Include all data sections from all files */ + } - .bss ALIGN (0x1000) : /* align at 4 KB */ - { - *(COMMON) /* all COMMON sections from all files */ - *(.bss) /* all bss sections from all files */ - } -} \ No newline at end of file + .bss ALIGN(0x1000) : { /* Align the .bss section to a 4 KB boundary */ + *(COMMON) /* Include all COMMON sections */ + *(.bss) /* Include all bss sections from all files */ + } +} diff --git a/Worksheet2/source/loader.asm b/Worksheet2/source/loader.asm new file mode 100644 index 0000000000000000000000000000000000000000..8fb92ee6aa70e479d62b862b93df850097f5ec72 --- /dev/null +++ b/Worksheet2/source/loader.asm @@ -0,0 +1,17 @@ +global loader ; the entry symbol for ELF + +MAGIC_NUMBER equ 0x1BADB002 ; Multiboot magic number +FLAGS equ 0x0 ; Multiboot flags +CHECKSUM equ -MAGIC_NUMBER ; Calculate checksum + +section .text +align 4 + dd MAGIC_NUMBER ; Multiboot header: magic number + dd FLAGS ; Multiboot header: flags + dd CHECKSUM ; Multiboot header: checksum + +loader: ; Entry point + mov eax, 0xCAFEBABE ; Move 0xCAFEBABE into EAX + +.loop: + jmp .loop ; Infinite loop to halt execution diff --git a/Worksheet2/source/loader.o b/Worksheet2/source/loader.o index 96049ae8116950800a73c4056d1fbf91b18ad877..77a0c8895f412835d6ed7210d4b37985b671e9e1 100644 Binary files a/Worksheet2/source/loader.o and b/Worksheet2/source/loader.o differ diff --git a/Worksheet2/source/loader.s b/Worksheet2/source/loader.s deleted file mode 100644 index 450ba9b1cbbd3290b297c3dc20c34677092098a3..0000000000000000000000000000000000000000 --- a/Worksheet2/source/loader.s +++ /dev/null @@ -1,42 +0,0 @@ -global loader ; the entry symbol for ELF - -MAGIC_NUMBER equ 0x1BADB002 ; define the magic number constant -FLAGS equ 0x0 ; multiboot flags -CHECKSUM equ -MAGIC_NUMBER ; calculate the checksum - ; (magic number + checksum + flags should equal 0) - -section .text ; start of the text (code) section -align 4 ; the code must be 4 byte aligned - dd MAGIC_NUMBER ; write the magic number to the machine code, - dd FLAGS ; the flags, - dd CHECKSUM ; and the checksum - -KERNEL_STACK_SIZE equ 4096 ; size of stack in bytes - -section .bss -align 4 ; align at 4 bytes -kernel_stack: ; label points to beginning of memory - resb KERNEL_STACK_SIZE ; reserve stack for the kernel - -section .text ; ensure all code is in .text -loader: ; the loader label (defined as entry point in linker script) - mov eax, 0xCAFEBABE ; place the number 0xCAFEBABE in the register eax - mov esp, kernel_stack + KERNEL_STACK_SIZE ; point esp to the start of the - ; stack (end of memory area) - - push dword 3 - push dword 2 - push dword 1 - call sum_of_three ; call the sum_of_three function - -.loop: - jmp .loop ; loop forever - -sum_of_three: - pop ebx ; Get first argument (this was pushed last) - pop ecx ; Get second argument - pop edx ; Get third argument - add eax, ebx ; Compute sum in eax - add eax, ecx - add eax, edx - ret ; Return diff --git a/Worksheet2/source/logQ.txt b/Worksheet2/source/logQ.txt index ff43ca25c78faa4649fbf5107d16bac041bc76c1..cb5987d92783994791b88c4eb25d16b24f25c39a 100644 Binary files a/Worksheet2/source/logQ.txt and b/Worksheet2/source/logQ.txt differ diff --git a/Worksheet2/source/os.iso b/Worksheet2/source/os.iso index 94bd5284a8fa064680f09cd39d9d55052651506d..b5402ce870e6284bd80925e631c871826d376a5a 100644 Binary files a/Worksheet2/source/os.iso and b/Worksheet2/source/os.iso differ