Friday, 26 July 2013

Operating System - Print a Custom string in Boot Loader Screen

When we power on the computer we will note that computer do a self test that is known as POST  (Power On Self Test), Which have so many activities including search for bootable device.

Let we try a sample boot loader which will print custom string as output.

NASM is used to assembler the code.

Boot Sector with Byte Sequence 0x55, 0xAA , When we see this in bytes 511, 512.Loader which contains these two then only it is a valid boot loader.

Boot Sector  is loaded into memory location  , Normally in location 0, 0x7c00 some bios are loaded into 0x7c00 to 0

[BITS 16]          - by this we are indicating the assembler that this is 16 bit.
[ORG 0x7C00]  - by this we are indicating the assembler where the code will be in memory after  loaded

1.  JMP $ - Means jump to the same location that means goes for infinite loop
2.  Times 510 -($ - $$) - A boot loader is always 512 bytes , so we need to resize of memory using Times                Directive $ stands for start of instruction and $$ stands for start of program . ($ - $$) Length of our                        program.
3.  DW 0xAA55 indicates boot signature. if this is not present that indicate this in invalid boot loader.

For printing we will use BIOS video interrupt int 0x10.
To use this interrupt we need to set some values for following  register.

AL  -    ASCII Value of character to display
AH  -   0x0E, What character we want to print on screen
BL   -   Text Attribute (Forground and Background) 0x07
BH  -    Page number 0x00

Print a String in Boot Loader:
[BITS 16]
[ORG 0x7C00]

MOV  SI, Hello
CALL String
JMP    $

MOV BH,0x00
MOV BL,0x07
INT    0x10               ; Call video interrupt
RET                          ; Return to called procedure


OR     AL,AL            ; check AL value is 0
JZ       exit_function    ; IF End then return
CALL Print                ; Else Print Char
JMP    Next
exit_function:               ; End Lablel
RET                            ; Return


Hello  db  'Hello  Rajesh', 0   ; Hello Rajesh string ending with 0

TIMES 510 - ($ - $$)  db 0
DW  oxAA55


Try Compile using NASM 
nasm bootloader.asm -f bin -o boot.bin

Try Copy to floppy
partcopy boot.bin 0 200 -fo                  - Windows user
dd if=boot .bin  bs=512  of=/dev/fdo    - Linux user , Insert the floppy don't mount it


From this article we can learn how to create a basic boot loader and print our string in the system boot.I hope this will help all of them to understand clearly about boot loader.