Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Lab 2: Implementation of a bootloader

Theory link: 1.4 Interrupts, design & booting

Experiment 2 of 15 Module 1 2 hours

Video walkthrough

Lab 2 walkthrough — Implementation of a bootloader

Video coming soon

Lab 2 walkthrough — Implementation of a bootloader

Recorded by Dr. Krithika L.B. Slot lab02-walkthrough

Aim

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.

Explanation

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):

  1. After the POST, the BIOS reads the first 512-byte sector of the boot disk.
  2. It checks that bytes 510 and 511 are 0x55 and 0xAA (the boot signature).
  3. It copies the sector to memory address 0x7C00 and jumps there.
  4. The CPU is in 16-bit real mode: there is no operating system, no printf, no files — only the BIOS services, reached through software interrupts (traps, Session 1.4).
Printing without an OS

BIOS interrupt int 0x10 with AH = 0x0E ("teletype output") prints the character in AL at the cursor and advances it.

org 0x7C00

Tells the assembler that the code will run at address 0x7C00, so the address of the message is calculated correctly.

times 510-($-$$) db 0

Pads the file with zeros up to byte 510. $ = current position, $$ = start of the section.

dw 0xAA55

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 AA

The 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.

Why is there also C code?

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.

Code

Program 1 · The boot sector (16-bit x86 assembly, NASM syntax)

boot.asm
; 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

Program 2 · Boot-image checker (C)

bootcheck.c
/* 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;
}

How to run

  1. Install the tools (once): sudo apt update && sudo apt install nasm qemu-system-x86 gcc
  2. Assemble the boot sector into a flat binary (no headers, just bytes): nasm -f bin boot.asm -o boot.bin
  3. Check its size: ls -l boot.bin must show exactly 512 bytes.
  4. Compile and run the checker: gcc bootcheck.c -o bootcheck then ./bootcheck boot.bin
  5. Optional — look at the raw bytes: xxd boot.bin | head -3 and xxd boot.bin | tail -1
  6. Boot it in the emulator: qemu-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.
Terminal
$ 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.bin
Troubleshooting
  • No graphical window (SSH, some WSL set-ups): BIOS text goes to the emulated VGA screen, so run qemu-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.
  • "No bootable device": the signature is missing or the file is not 512 bytes — run ./bootcheck boot.bin.
  • Garbage characters: [org 0x7c00] is missing, so the message address is wrong.

Sample input & output

bootcheck on the assembled image

Input
boot.bin produced by NASM
Output
Output
$ ./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 on a broken image

Input
boot.asm with the last line (dw 0xAA55) removed and the padding changed to 512
Output
Output
$ ./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 with no argument

Input
(none)
Output
Output
$ ./bootcheck
Usage: ./bootcheck boot.bin

The raw bytes

Input
xxd boot.bin
Output
Output
$ 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.

QEMU window after booting boot.bin

Input
qemu-system-x86_64 -drive format=raw,file=boot.bin
Output
Output
SeaBIOS (version 1.15.0-1)
iPXE (https://ipxe.org) ...
Booting from Hard Disk...
Hello from my bootloader! - BACSE106

Try it yourself

  1. Change the message to print your name and register number. Re-assemble and boot.
  2. Print a second line by adding another string and a second print loop (or turn the loop into a subroutine with call/ret).
  3. Read a key with BIOS int 0x16 (AH = 0; the character returns in AL) and echo every key the user types.
  4. Extend bootcheck.c to print the message text it finds in the image (bytes 31 onward until the 0 byte).
  5. Remove dw 0xAA55, pad to 512 bytes, and boot again. What does the BIOS say? Why?

Viva questions

Prepare these for the lab evaluation.

Revealed 0
At which address does the BIOS load the boot sector, and how big is it?

At physical address 0x7C00; the sector is 512 bytes.

Why is the signature written as 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).

Why can't we use 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.

What does -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.

How does a real boot loader like GRUB fit in 446 bytes?

It doesn't — stage 1 in the MBR only loads a bigger second stage from disk, which understands file systems and loads the kernel.

What is the difference between BIOS and UEFI booting?

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).