You are on page 1of 7

Writing the Boot Sector and make it executes a simple program.

Shady Elwan Thebes Academy Computer Science – Level 3

دخخبع ينوخخع روتكدخخلل صاخخخ ركش

بتكا يلعجو يل هزيفحتل يداهلا هباخخختكل فاخخخضالاب لاخخخقملا اذخخخه

Before we start, we must know some facts about the topic

The Boot Sector is 512 bytes in size

The Boot Sector MUST end with 2 special bytes called

"boot

sector signature" ,0XAA55

The Boot Sector ,when written to any bootable drive (hard

disk, floppy, cd drive,

..

etc) ,it must be written to

Sector 1 Cylinder 0 Head 0
Sector 1
Cylinder 0
Head 0

So when we turned on the pc and after the POST operation the bios check the list of the bootable devices and access the first device in the list and checks Head(0)=>Cylinder(0)=>Sector(1) of the device and see if the "the boot sector signature" (x0AA55) exist at offset 510 if not it will make the same check on the next device in the list and so on,….if the bios doesn't find the "boot sector signature" it will display an error message on a black screen otherwise it will load the first sector at memory location 0x7c00 and executes it.

The Boot Sector Code

[BITS 16]

; the bios starts out in 16-bit real mode

[ORG 0]

; Data offset = 0

jmp START

; Skip Data section and go to the begin of our simple program

;============================== Data ==================================================

welcomeMessage

db 'The bootable program loaded successfully!!',13,10,13,10

db 'About Me:',13,10

db 'Shadi Elwan',13,10

db 'Thebes Academy',13,10

db 'Computer Science - Level 3',13,10,13,10,0

rebootMessage

db 'Press any key to reboot.',0

;============================== Data ==================================================

;

=======================================================

===============================

;============================== Functions ==============================================

ShowMessage:

mov al,[si]

;load byte at location DS:SI into al

cmp al,0

;If al register equals 0 that means we reache the end of the String

and set "Zero Flag" to 1

jz EndFunction ;if "Zero Flag" equals 1 then go to EndFunction

mov ah,0eh

;;We will call interrupt 10 (video interrupt)

int 0x10

;;so we make ah=0eh to tell the bios to put (al) value into the screen

inc si

;;Increase SI by 1 so we point now to the next character

jmp ShowMessage

EndFunction:

ret

;return from this function to the caller

;

=======================================================

================================

START:

;

=======================================================

================================

mov ax,0x7c0

;; BIOS puts our code at memory location 0:07C00h, so to be

mov ds,ax

;; able to access our Data we set Data Segmant (ds) = 0x7c0

;

=======================================================

================================

;===================== Our Real program starts from here =======================

mov si,welcomeMessage

call ShowMessage

mov si,rebootMessage

call ShowMessage

;; wait for key press

mov ah,0

int 0x16

;then reboot!

JMP 0FFFFh:0000h

;========================= Our Real program ends here ==========================

;

=======================================================

================================

times 510-($-$$) db 0

;Fill The file with 0's untill its size reaches 510 bytes

dw 0xAA55

;Put these 2 bytes at the end of file [Boot Sector Signature]

;(now the file size is 512 bytes)

;

=======================================================

================================

End of Code

How I make it works

First I use nasm tool to compile the code to plain binary file using this command:

Nasm -f bin mybootCode.asm -o ShadiBootCode.bin Where:

mybootCode.asm: Is the assembly code file. ShadiBootCode.bin: Is the output compiled binary file.

Second I used a program called UltraISO to put the compiled

code to the sector 1 of a cd image then I burned this image to a real cd disk and voila it works 

First create empty cd image

First create empty cd image

First create empty cd image
Then load the boot file

Then load the boot file

Then load the boot file

Select Burn CD/DVD Image

After burning the cd image to a real cd disk put the disk intto your pc and reboot and make sure that your pc will boot from cd drive first and you will see something like this

Select Burn CD/DVD Image After burning the cd image to a real cd disk put the

Best regards: Shady Elwan