Create a Hello world 16 bit boot loader

Booting Process

   When a computer  starts up ( obviously by pressing the power button), the first thing that occurs is it  send a signal to motherboard which in turn starts the power supply. After supplying the correct amount of power to each device, it send a signal called “Power OK” to BIOS which resides on motherboard.

Once the BIOS receive the “Power OK” signal, it starts the booting process by first initializing a process called POST (Power On Self Test). POST first check that every device has right amount of power and then it check whether the memory is not corrupted. Then it initialize each devices and finally it gives control to BIOS for further booting.

Now the final process of booting begins. For this the BIOS first find 512 bytes of image called MBR (Master Boot Record) or Bootsector from the floppy disk or hard disk which is used for booting. The priority of boot devices is set by the user in BIOS setting. The normal priority is floppy disk first, then hard disk.

Once BIOS finds the bootsector it loads the image in memory and execute it. If a valid bootsector is not found, BIOS check for next drive in boot sequence until it find valid bootsector. If BIOS fails to get valid bootsector, generally it stops the execution and gives an error message “Disk boot failure”.

It is bootsectors responsibility to load the operating system in memory and execute it.

NASM

The assembler used here is NASM (Netwide Assembler). NASM is an 80×86 assembler designed for portability and modularity. It supports a range of object file formats, including Linux and NetBSD/FreeBSD a.out, ELF, COFF, Microsoft 16-bit OBJ and Win32. It will also output plain binary files. Its syntax is designed to be simple and easy to understand, similar to Intel’s but less complex. It supports Pentium, P6, MMX, 3DNow!, SSE and SSE2 opcodes, and has macro capability.

Download and install

NASM (http://www.nasm.us/)
Virtual Box (https://www.virtualbox.org/)

Creating Bootloader

This is the step to code our bootloader. Take terminal and make a asm file.

Vim ourbootloader.asm

[BITS 16]       ;Tells the assembler that its a 16 bit code
[ORG 0x7C00]    ;Origin, tell the assembler that where the code will
                                ;be in memory after it is been loaded

MOV SI, HelloString ;Store string pointer to SI
CALL PrintString        ;Call print string procedure
JMP $           ;Infinite loop, hang it here.

PrintCharacter: ;Procedure to print character on screen
        ;Assume that ASCII value is in register AL
MOV AH, 0x0E    ;Tell BIOS that we need to print one charater on screen.
MOV BH, 0x00    ;Page no.
MOV BL, 0x07    ;Text attribute 0x07 is lightgrey font on black background

INT 0x10        ;Call video interrupt
RET             ;Return to calling procedure

PrintString:    ;Procedure to print string on screen
        ;Assume that string starting pointer is in register SI

next_character: ;Lable to fetch next character from string
MOV AL, [SI]    ;Get a byte from string and store in AL register
INC SI          ;Increment SI pointer
OR AL, AL       ;Check if value in AL is zero (end of string)
JZ exit_function ;If end then return
CALL PrintCharacter ;Else print the character which is in AL register
JMP next_character      ;Fetch next character from string
exit_function:  ;End label
RET             ;Return from procedure

;Data
HelloString db 'Hello World', 0 ;HelloWorld string ending with 0

TIMES 510 - ($ - $$) db 0       ;Fill the rest of sector with 0
DW 0xAA55                       ;Add boot signature at the end of bootloader

1. Create a virtual floppy image that will contain the bootloader.

     dd if=/dev/zero of=ourbootloader.img bs=512 count=2880 

2. Compile the file

     nasm -f bin -o ourbootloader.bin ourbootloader.asm 

3. Copy the bin file's content to the img file.

     dd status=noxfer conv=notrunc if=ourbootloader.bin of=ourbootloader.img 

4. Open VirtualBox and create a new machine.

     Operating System: Other
     Version: DOS

5.After creating machine , change settings: take storage from settings and add controller(floppy controller) and 
  open the img file created earlier.

6.Choose the virtual floppy image and open it, simply start the machine and it will display the message.