Lab 2 walkthrough — Implementation of a bootloader
Lab 2 walkthrough — Implementation of a bootloader
Recorded by Dr. Krithika L.B. Slot lab02-walkthrough
Theory link: 1.4 Interrupts, design & booting
Lab 2 walkthrough — Implementation of a bootloader
Recorded by Dr. Krithika L.B. Slot lab02-walkthrough
Aim. To write a minimal x86 boot sector that the BIOS loads and executes, displaying a message on the screen, and to write a C program that verifies the boot image the way the BIOS does.
After this lab you can: explain where and how the BIOS loads a boot sector; describe the 512-byte layout and the 0x55 0xAA signature; assemble a flat binary and boot it in an emulator.
Requirements: Linux (or WSL) with nasm, gcc and qemu-system-x86.
Session 1.4 described the boot chain: firmware → boot sector → boot loader → kernel → init. In this lab we build the second link ourselves. On a legacy-BIOS PC (and in the QEMU emulator):
0x55 and 0xAA (the boot signature).0x7C00 and jumps there.printf, no files — only
the BIOS services, reached through software interrupts (traps, Session 1.4).BIOS interrupt int 0x10 with AH = 0x0E ("teletype output") prints the character in AL at the cursor and advances it.
Tells the assembler that the code will run at address 0x7C00, so the address of the message is calculated correctly.
Pads the file with zeros up to byte 510. $ = current position, $$ = start of the section.
Stores the signature. x86 is little-endian, so the bytes on disk are 55 AA.
Layout of boot.bin (exactly 512 bytes)
bytes 0 – 30 · code bytes 31 – 69 · message text bytes 70 – 509 · zero padding 510 – 511 · 55 AAThe C program in this lab plays the role of the BIOS's check: it opens the image, confirms it is exactly 512 bytes, prints the first bytes of machine code, and tests the signature. This shows that a “bootloader” is nothing magical — it is just bytes placed where the firmware expects them.
A real boot sector must be written in assembly: when it runs there is no C runtime, no stack set up by an OS and only 510 bytes of space. Real boot loaders such as GRUB use a tiny assembly stage that then loads a larger stage written in C. Our bootcheck.c is an ordinary Linux C program that inspects the image.
; boot.asm - a minimal boot sector that prints a message (BACSE106 Lab 2)
[bits 16] ; the BIOS starts us in 16-bit real mode
[org 0x7c00] ; and loads this sector at address 0x7C00
start:
xor ax, ax ; AX = 0
mov ds, ax ; data segment = 0, so [msg] = 0x0000:msg
mov es, ax
mov ss, ax
mov sp, 0x7c00 ; stack grows downward from below our code
mov si, msg ; SI points to the first character
print:
lodsb ; AL = next character, SI = SI + 1
cmp al, 0 ; end of string?
je hang
mov ah, 0x0e ; BIOS teletype function
mov bh, 0 ; display page 0
int 0x10 ; call the BIOS video service
jmp print
hang:
cli ; disable interrupts
hlt ; halt the CPU
jmp hang ; stay here forever
msg db "Hello from my bootloader! - BACSE106", 13, 10, 0
times 510-($-$$) db 0 ; pad with zeros up to byte 510
dw 0xAA55 ; boot signature: bytes 55 AA/* bootcheck.c - verify a boot sector image the way the BIOS does (BACSE106 Lab 2) */
#include <stdio.h>
int main(int argc, char *argv[])
{
unsigned char sector[1024];
FILE *fp;
int n, i;
if (argc != 2) {
printf("Usage: %s boot.bin\n", argv[0]);
return 1;
}
fp = fopen(argv[1], "rb");
if (fp == NULL) {
printf("Cannot open %s\n", argv[1]);
return 1;
}
n = fread(sector, 1, sizeof(sector), fp);
fclose(fp);
printf("Image size : %d bytes\n", n);
printf("First 16 bytes : ");
for (i = 0; i < 16 && i < n; i++)
printf("%02X ", sector[i]);
printf("\n");
if (n != 512) {
printf("Result : INVALID - a boot sector must be exactly 512 bytes\n");
return 1;
}
printf("Bytes 510, 511 : %02X %02X\n", sector[510], sector[511]);
if (sector[510] == 0x55 && sector[511] == 0xAA)
printf("Result : VALID boot sector - BIOS will load it at 0x7C00\n");
else
printf("Result : INVALID - boot signature 55 AA is missing\n");
return 0;
}sudo apt update && sudo apt install nasm qemu-system-x86 gccnasm -f bin boot.asm -o boot.binls -l boot.bin must show exactly 512 bytes.gcc bootcheck.c -o bootcheck then ./bootcheck boot.binxxd boot.bin | head -3 and xxd boot.bin | tail -1qemu-system-x86_64 -drive format=raw,file=boot.bin. A QEMU window opens, the BIOS runs and your message appears. Close the window to stop.$ nasm -f bin boot.asm -o boot.bin
$ ls -l boot.bin
-rw-r--r-- 1 student student 512 Jan 13 10:02 boot.bin
$ gcc bootcheck.c -o bootcheck
$ ./bootcheck boot.bin
$ qemu-system-x86_64 -drive format=raw,file=boot.binqemu-system-x86_64 -drive format=raw,file=boot.bin -display curses to see it inside the terminal. To quit, press Alt+2 (or Esc then 2) to reach the QEMU monitor and type quit../bootcheck boot.bin.[org 0x7c00] is missing, so the message address is wrong.$ ./bootcheck boot.bin
Image size : 512 bytes
First 16 bytes : 31 C0 8E D8 8E C0 8E D0 BC 00 7C BE 1F 7C AC 3C
Bytes 510, 511 : 55 AA
Result : VALID boot sector - BIOS will load it at 0x7C00$ ./bootcheck bad.bin
Image size : 512 bytes
First 16 bytes : 31 C0 8E D8 8E C0 8E D0 BC 00 7C BE 1F 7C AC 3C
Bytes 510, 511 : 00 00
Result : INVALID - boot signature 55 AA is missing$ ./bootcheck
Usage: ./bootcheck boot.bin$ xxd boot.bin | head -2
00000000: 31c0 8ed8 8ec0 8ed0 bc00 7cbe 1f7c ac3c 1.........|..|.<
00000010: 0074 08b4 0eb7 00cd 10eb f3fa f4eb fc48 .t.............H
$ xxd boot.bin | tail -1
000001f0: 0000 0000 0000 0000 0000 0000 0000 55aa ..............U.SeaBIOS (version 1.15.0-1)
iPXE (https://ipxe.org) ...
Booting from Hard Disk...
Hello from my bootloader! - BACSE106call/ret).int 0x16 (AH = 0; the character returns in AL) and echo every key the user types.bootcheck.c to print the message text it finds in the image (bytes 31 onward until the 0 byte).dw 0xAA55, pad to 512 bytes, and boot again. What does the BIOS say? Why?Prepare these for the lab evaluation.
Revealed 0At physical address 0x7C00; the sector is 512 bytes.
dw 0xAA55 but checked as bytes 55 AA?x86 is little-endian: the low byte 0x55 is stored first (offset 510), then 0xAA (offset 511).
printf in the boot sector?No OS or C library is loaded yet. The only services available are the BIOS routines reached with int instructions.
-f bin tell NASM?Produce a flat binary — raw machine code with no ELF/EXE headers — because the BIOS executes the bytes exactly as they are.
It doesn't — stage 1 in the MBR only loads a bigger second stage from disk, which understands file systems and loads the kernel.
BIOS loads a 512-byte sector in 16-bit mode; UEFI loads an EFI application file from the EFI System Partition in 32/64-bit mode and can verify signatures (Secure Boot).