├── LICENSE ├── bootloader.asm └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Jan Cipher 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /bootloader.asm: -------------------------------------------------------------------------------- 1 | org 0x7c00 ; Set origin to 0x7c00 2 | 3 | bits 16 ; 16-bit real mode 4 | 5 | start: 6 | jmp main ; Jump to main code 7 | 8 | ; Bootloader code starts here 9 | 10 | main: 11 | mov ax, 0x07C0 ; Set up data segment 12 | mov ds, ax 13 | mov es, ax 14 | 15 | mov si, hello_msg ; Print a welcome message 16 | call print_string 17 | 18 | mov si, prompt_msg ; Print a prompt message 19 | call print_string 20 | 21 | call read_char ; Read a character from keyboard 22 | 23 | mov si, newline ; Move cursor to the next line 24 | call print_string 25 | 26 | mov si, echo_msg ; Print the character that was entered 27 | call print_string 28 | 29 | call print_char ; Print the character 30 | 31 | jmp $ ; Infinite loop 32 | 33 | ; Print a null-terminated string 34 | print_string: 35 | lodsb ; Load the next character 36 | or al, al ; Check for null terminator 37 | jz end_print_string ; If null, end of string 38 | 39 | call print_char ; Print the character 40 | jmp print_string ; Repeat for the next character 41 | 42 | end_print_string: 43 | ret 44 | 45 | ; Print a character in AL 46 | print_char: 47 | mov ah, 0x0E ; BIOS teletype function 48 | int 0x10 ; Call BIOS interrupt 49 | 50 | ret 51 | 52 | ; Read a character from the keyboard 53 | read_char: 54 | mov ah, 0 ; BIOS keyboard input function 55 | int 0x16 ; Call BIOS interrupt 56 | 57 | ret 58 | 59 | ; Data section 60 | 61 | hello_msg db "Welcome to My Bootloader!", 0 62 | prompt_msg db "Enter a character: ", 0 63 | echo_msg db "You entered: ", 0 64 | newline db 0x0D, 0x0A, 0 ; CR LF null terminator 65 | 66 | times 510-($-$$) db 0 ; Pad the bootloader to 510 bytes 67 | dw 0xAA55 ; Boot signature at 511-512 bytes 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Bootloader 2 | 3 | This is a simple bootloader written in NASM assembly language. It is designed to be placed in the first sector of a bootable disk and executed during the boot process. The bootloader displays a welcome message, prompts the user to enter a character, and then displays the entered character on the screen. 4 | 5 | ## Prerequisites 6 | 7 | To assemble the bootloader code, you need to have NASM (Netwide Assembler) installed on your system. You can download NASM from the official website: [https://www.nasm.us/](https://www.nasm.us/) 8 | 9 | ## Building the Bootloader 10 | 11 | To assemble the bootloader code, follow these steps: 12 | 13 | 1. Save the provided code in a file called `bootloader.asm`. 14 | 2. Open a terminal or command prompt. 15 | 3. Navigate to the directory where the `bootloader.asm` file is located. 16 | 4. Run the following command to assemble the code: 17 | 18 | ``` 19 | nasm -f bin bootloader.asm -o bootloader.bin 20 | ``` 21 | 22 | This command will use NASM to assemble the code into a binary file called `bootloader.bin`. 23 | 24 | ## Running the Bootloader 25 | 26 | Once you have the `bootloader.bin` file, you can run it using an emulator or write it to the first sector of a bootable disk. 27 | 28 | ### Running with an Emulator 29 | 30 | You can use an x86 emulator, such as QEMU, to run the bootloader. Follow these steps: 31 | 32 | 1. Install QEMU on your system if you haven't already. You can download QEMU from the official website: [https://www.qemu.org/](https://www.qemu.org/) 33 | 2. Open a terminal or command prompt. 34 | 3. Navigate to the directory where the `bootloader.bin` file is located. 35 | 4. Run the following command to start the emulator and execute the bootloader: 36 | 37 | ``` 38 | qemu-system-x86_64 -drive format=raw,file=bootloader.bin 39 | ``` 40 | 41 | This command will launch the emulator and load the `bootloader.bin` file as the bootable disk. 42 | 43 | ### Writing to a Bootable Disk 44 | 45 | If you want to run the bootloader on real hardware, you can write the `bootloader.bin` file to the first sector of a bootable disk, such as a USB drive. Be cautious when performing disk operations, as it can erase or damage existing data. 46 | 47 | **Important: Writing to a disk will erase its contents. Make sure to back up any important data before proceeding.** 48 | 49 | Follow these steps to write the bootloader to a bootable disk: 50 | 51 | 1. Insert a blank bootable disk, such as a USB drive, into your computer. 52 | 2. Open a terminal or command prompt. 53 | 3. Identify the device name of the bootable disk. On Linux, you can use the `lsblk` command or the `fdisk -l` command to list available disks. On Windows, you can use the `diskpart` utility or third-party tools like Rufus. 54 | 4. Make sure you have the correct device name and double-check that it is the desired bootable disk. 55 | 5. Run the following command to write the bootloader to the bootable disk (replace `/dev/sdx` with the correct device name): 56 | 57 | ``` 58 | dd if=bootloader.bin of=/dev/sdx bs=512 count=1 59 | ``` 60 | 61 | This command will use the `dd` utility to write the `bootloader.bin` file to the first sector of the bootable disk. 62 | 63 | **Note:** Be extremely careful to specify the correct device name to avoid accidentally overwriting important data. 64 | 65 | ## License 66 | 67 | This code is provided under the MIT License. See the [LICENSE](LICENSE) file for details. 68 | --------------------------------------------------------------------------------