Lab Exercise 1: Boot Loader

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Lab Exercise 1: Boot Loader

Objective
• To learn how an operating system is loaded in
the main memory

• To learn how to write a boot loader


What is a boot loader?
• It’s a special type of loader to load the
OS/system software in to main memory
What is a bootable device?
• A device is bootable device if it contains a
boot sector or boot block

• BIOS reads that device by first loading the


boot sector into memory for execution
What is a boot sector?
• It’s the sector of the bootable device that
contains code for loading the device
BIOS
• BIOS is usually a ROM chip in the motherboard
• It contains assembly level code to talk to the I/O devices, disks connected
to the system
• OS can use the I/O devices by generating an interrupt to the BIOS
• BIOS can be interrupted by giving “int 0x10” assembly code
– “int” is the opcode for calling an interrupt and 0x10 is the number for BIOS
(Control transfer to BIOS)
• Within BIOS there are several interrupt service routines. Where will the
control actually jump to?
– It depends on the interrupt vector or service number passed in the register “ah”
– Whenever “int 0x10” is passed, immediately the value of “ah” will be checked
– Accordingly the Interrupt Service Routine (ISR) will be chosen
– Depending on the ISR chosen, other registers may be investigated (If needed)
• Discussing BIOS in detail is beyond the scope of this course
How to write a Boot Sector?
• A boot sector is usually is of 512 bytes

• A boot sector must contain “0x55” as its 510th byte and “0xaa”
as its 511th byte

• Always the boot code should be compiled and assembled to


contain only 16 bit instruction (irrespective of the (32 /64) bit
architecture/OS)

• If a device does not follow the above rules, then it is not


considered to be bootable
Steps to write a Boot-loader
1. Write in assembly (depending on the architecture), piece of
code to make the 510 and 511th bytes to be 0x55 and 0xaa.
2. In the assembly code you can use any other BIOS interrupts
to print/get input to/from the IO device
3. After the assembly code is written, assemble it as .bin file
4. Write the .bin file as a floppy boot
In Linux use “dd” command for the purpose
5. Create a disk image of the bin file
6. Use the disk image to boot
Difference between object file and binary file

• Object file is in hexadecimal and still is not in


the executable format. External references are
not included in the object file

• Bin are executable formats that are in binary


format with all the external links resolved
Simple Boot Loader
Step 1:
Download NASM simulator for compiling and generating the executable for our
assembly code
Step 2:
Write the assembly code that clears 512bytes of memory and adds the magic number
at the end Represents format
syntax : nasm input_file –f bin –o output_file of the output file
nasm ourbootloader.asm -f bin -o boot.bin

Step 3:
Convert the bootable binary to bootable image using “dd” command
dd if=boot.bin bs=512 of=outputboot.img
Step 4:
Use the bootable image to start a new vm to check your bootloader
Sample Code
[BITS 16] ;tell 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
JMP $ ;infinite loop
TIMES 510 - ($ - $$) db 0 ;fill the rest of sector with 0
DW 0xAA55 ; add boot signature at the end
of bootloader
Example 2 :Print a Character BootLoader

• INT 0x10 (BIOS Interrupt to print on screen)


– This interrupt will check the register AL for the
character to display
– AH register will contain the number of data to
display on screen
– BL register will contain the details about back
ground and fore ground colour
Sample Code
[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 AL, 65
CALL PrintCharacter
JMP $ ;Infinite loop, hang it here.
PrintCharacter: ;Procedure to print character on screen
MOV AL, 65 ; ASCII value of ‘A’
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 interrupt
RET ;Return to calling procedure

TIMES 510 - ($ - $$) db 0 ;Fill the rest of sector with 0


DW 0xAA55 ;Add boot signature at the end of bootloader
Example 3: Print Hello World
[BITS 16] ;Tells the assembler that its PrintString: ;Procedure to print string on
a 16 bit [ORG 0x7C00] ;Origin screen
next_character: ;Lable to fetch next
character MOV AL, [SI] ;Get a byte from
MOV SI, HelloString ;Store string string
pointer to SI
INC SI ;Increment SI pointer
CALL PrintString ;Call print string OR AL, AL ;Check if value in AL is
procedure zero (end of string)
JMP $ ;Infinite loop, hang JZ exit_function ;If end then return
it CALL PrintCharacter
here. JMP next_character ;Fetch next
PrintCharacter: ;Procedure to print character exit_function: ;End label
MOV AH, 0x0E RET ;Return from procedure
MOV BH, 0x00
MOV BL, 0x07 ;Data
INT 0x10 ;Call video interrupt HelloString db 'Hello World', 0
RET ;Return to calling procedure TIMES 510 - ($ - $$) db 0
DW 0xAA55
HOT Question
• Write a boot loader to load DOS

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy