Last active
August 29, 2015 14:16
-
-
Save ameenkhan07/ee8a2cfad1b36f1c5f42 to your computer and use it in GitHub Desktop.
16 bit assembly bootstrapped loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bits 16 ; 16-bit Real Mode | |
org 0x7c00 ; BIOS boot origin | |
jmp main ;jump to MAIN function | |
;Print-Variables | |
Message db "MY OWN BOOTLOADER...", 0x0 | |
MessageB db "Written in x86 assembly language...", 0x0 | |
AnyKey db "Press (ctrl+c) key to reboot...", 0x0 | |
;Function to print characters to the screen | |
Println: | |
lodsb ;Load string | |
or al, al | |
jz complete | |
mov ah, 0x0e | |
int 0x10 ;BIOS Interrupt 0x10 - Used to print characters on the screen via Video Memory | |
jmp Println ;Loop | |
complete: | |
call PrintNwL | |
;Function to print new lines like '\n' in C/C++ | |
PrintNwL: | |
mov al, 0 ; null terminator '\0' | |
stosb ; Store string | |
;Adds a newline break '\n' | |
mov ah, 0x0E | |
mov al, 0x0D | |
int 0x10 | |
mov al, 0x0A | |
int 0x10 | |
ret | |
;Function to reboot the Machine | |
Reboot: | |
;Sends us to the end of the memory | |
;causing reboot | |
db 0x0ea | |
dw 0x0000 | |
dw 0xffff | |
;Function which gets the pressed key from the keyboard | |
GetPressedKey: | |
mov ah, 0 | |
int 0x16 ;BIOS Keyboard Service | |
ret | |
;Bootloader entry-code | MAIN FUNCTION | |
main: | |
cli ;Clear interrupts | |
;Setup stack segments | |
mov ax,cs | |
mov ds,ax | |
mov es,ax | |
mov ss,ax | |
sti ;Enable interrupts | |
call PrintNwL | |
call PrintNwL | |
call PrintNwL | |
call PrintNwL | |
call PrintNwL | |
;Print the first characters | |
mov si, Message | |
call Println | |
mov si, MessageB | |
call Println | |
mov si, AnyKey | |
call Println | |
label: call GetPressedKey | |
cmp al,03h | |
jnz label | |
call Reboot | |
times 510 - ($-$$) db 0 ;Fill the rest of the bootloader with zeros | |
dw 0xAA55 ;Boot signature |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. compile the above code using nasm using the command : | |
nasm -f bin boot.asm -o boot.img | |
2. make a bootable usb of the image file created above. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment