From 554ada6fc34d8b1f43c062ddce6f6f12c2cde1d7 Mon Sep 17 00:00:00 2001 From: heckeralt Date: Sat, 5 Oct 2024 12:46:22 -0400 Subject: [PATCH] Add boot.asm --- boot.asm | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 boot.asm diff --git a/boot.asm b/boot.asm new file mode 100644 index 0000000..d1f6c89 --- /dev/null +++ b/boot.asm @@ -0,0 +1,120 @@ +[bits 16] ; Set to 16-bit mode +[org 0x7C00] ; BIOS loads the bootloader at this address + +; Define GDT +gdt_start: + dw 0x0000 ; Limit (low) + dw 0x0000 ; Base (low) + db 0x00 ; Base (mid) + db 0x9A ; Access byte: Present, ring 0, executable + db 0xCF ; Flags: Granularity, 32-bit + db 0x00 ; Base (high) + + dw 0x0000 ; Limit (low) + dw 0x0000 ; Base (low) + db 0x00 ; Base (mid) + db 0x92 ; Access byte: Present, ring 0, writable + db 0xCF ; Flags: Granularity, 32-bit + db 0x00 ; Base (high) + +gdt_descriptor: + dw gdt_end - gdt_start - 1 ; Limit + dd gdt_start ; Base address + +gdt_end: + +start: + cli ; Disable interrupts + lgdt [gdt_descriptor] ; Load GDT + mov eax, cr0 ; Read CR0 register + or eax, 1 ; Set PE bit (Protected Mode Enable) + mov cr0, eax ; Write back to CR0 + + jmp 0x08:protected_mode_entry ; Far jump to flush the pipeline + +protected_mode_entry: + mov ax, 0 ; Set data segment to the new segment + mov ds, ax ; Load data segment register + mov es, ax ; Load extra segment register + mov fs, ax ; Load fs segment register + mov gs, ax ; Load gs segment register + + call clear_screen ; Clear the screen + mov si, welcome_message + call print_string_protected + +command_loop: + mov si, prompt_message + call print_string_protected + + call read_input + + cmp al, 'h' ; Check for 'h' command (help) + je print_help + cmp al, 'q' ; Check for 'q' command (quit) + je quit_terminal + cmp al, 'f' ; Check for 'f' command (heckerfetch) + je heckerfetch + + mov si, unknown_command_message + call print_string_protected + jmp command_loop ; Repeat the command loop + +print_help: + mov si, help_message + call print_string_protected + jmp command_loop ; Return to command loop + +heckerfetch: + mov si, heckerfetch_message + call print_string_protected + jmp command_loop ; Return to command loop + +quit_terminal: + mov si, goodbye_message + call print_string_protected + jmp hang ; Hang the system + +; Function to read a character from keyboard input +read_input: + xor eax, eax ; Clear EAX for reading character input + int 0x16 ; Call BIOS interrupt to read a character into AL + ret ; Return with character in AL + +; Function to print a string in protected mode using VGA text buffer +print_string_protected: + mov edi, 0xb8000 ; VGA text buffer address +.next_char: + lodsb ; Load byte at DS:SI into AL and increment SI + cmp al, 0 ; Check for null terminator + je .done ; If null, we are done + mov [edi], ax ; Write character and attribute (white on black) + add edi, 2 ; Move to next character cell + jmp .next_char ; Repeat for next character +.done: + ret ; Return from function + +; Function to clear the screen in protected mode +clear_screen: + xor di, di ; Start from the beginning of the VGA buffer + mov cx, 80 * 25 ; Number of characters in a standard text mode screen + mov ax, 0x07 ; Attribute for white on black background +.fill_screen: + stosw ; Store word in [ES:DI] and increment DI by 2 + loop .fill_screen ; Repeat until CX is zero + ret ; Return from function + +; Data section for messages +welcome_message db 'Welcome to the HeckerOS!', 0 +prompt_message db 'Enter command (h for help, q to quit, f for heckerfetch): ', 0 +help_message db 'Available commands: h (help), q (quit), f (heckerfetch)', 0 +unknown_command_message db 'Unknown command! Try again.', 0 +goodbye_message db 'Goodbye!', 0 +heckerfetch_message db 'OS: HeckerOS Beta, CPU: HeckerSoft Generic CPU 0GHZ', 'Host: HeckerSoft Unknown Host', 0 + +; Bootloader signature (must be present) +times 510 - ($ - $$) db 0 ; Fill the rest of the sector with zeros +dw 0xAA55 ; Boot signature + +hang: + jmp hang ; Jump to hang indefinitely \ No newline at end of file