Skip to content
systemcoding.dev
Assembly

Write a Boot Sector That Prints Hello in 16-Bit Real Mode

Write a 512-byte boot sector in NASM that sets up segments and a stack, prints a string with BIOS int 0x10 and ends with the 0xAA55 signature, line by line.

6 min read
Summarize in AI
Flat illustration of an assembly listing with addresses and mnemonics, with the Linux, Intel and GNU logos.

The smallest program a PC can run without an operating system is a boot sector: exactly 512 bytes, the last two of which are 0x55 and 0xAA. When the BIOS finds a drive whose first sector ends that way, it copies the 512 bytes to memory address 0x7C00 and jumps there. Nothing else is set up. There is no stack you can trust, no file system, no standard library and no operating system, only the BIOS routines reachable through software interrupts. This post writes one that prints a line of text, with NASM 2.16 on Linux, and explains every line, because each one is there for a reason that the next boot sector you write will also need. The output is a single boot.bin file of exactly 512 bytes. How to run it in an emulator is the subject of the next post in this series.

What the CPU gives you

A modern x86 processor, whatever its age, starts in real mode, the 16-bit mode of the original 8086. Registers are 16 bits wide (ax, bx, cx, dx, si, di, sp, bp) and addresses are built from a 16-bit segment and a 16-bit offset: the physical address is segment * 16 + offset. That gives a limit of 1 MiB, which is why real mode programs think in segments. The BIOS jumps to 0x7C00 and the value in the segment registers is not guaranteed. Some BIOSes jump to 0000:7C00 and others to 07C0:0000, which are the same physical address with different segment and offset, and the difference matters for any code that uses absolute offsets.

The program

Save this as boot.asm:

boot.asmnasm
bits 16
org 0x7C00

start:
    cli
    xor  ax, ax
    mov  ds, ax
    mov  es, ax
    mov  ss, ax
    mov  sp, 0x7C00
    sti

    mov  si, msg

print:
    lodsb
    test al, al
    jz   halt
    mov  ah, 0x0E
    mov  bh, 0
    mov  bl, 0x07
    int  0x10
    jmp  print

halt:
    cli
    hlt
    jmp  halt

msg: db "Hello from a boot sector!", 13, 10, 0

times 510 - ($ - $$) db 0
dw 0xAA55

Assemble it as a flat binary, which is what a boot sector is:

Shell
nasm -f bin boot.asm -o boot.bin

The directives

bits 16 tells NASM to emit 16-bit code, which is what real mode needs. org 0x7C00 tells it the code will run at address 0x7C00, so labels like msg get offsets that include that base. Without org, mov si, msg would load the offset from the start of the file, which is wrong at run time, and the program would print garbage from the wrong address.

The last two lines make the file exactly 512 bytes. times 510 - ($ - $$) db 0 repeats a zero byte as many times as it takes to fill the file to 510 bytes: $ is the address of the current line and $$ the start of the section, so $ - $$ is the number of bytes emitted so far. Then dw 0xAA55 writes a 16-bit word. x86 is little-endian, so the bytes in the file are 55 AA, in that order, and the BIOS checks for exactly that pair in bytes 510 and 511. If the program is longer than 510 bytes, times gets a negative count and NASM reports an error, so the build fails instead of producing a bad sector.

Segments and the stack

The first block makes no assumptions. cli disables interrupts while the stack changes, because an interrupt that arrived between setting ss and sp would push onto a nonsense stack. xor ax, ax zeroes ax in two bytes, and the next three lines set the data, extra and stack segments to 0. With ds = 0 and org 0x7C00, an address like msg means physical address 0x7C00 + offset, matching how the code was assembled. mov sp, 0x7C00 puts the top of the stack at 0x7C00, directly below the boot sector. The stack grows downward, so it uses the memory below the code, which is free on a PC, and it never overlaps the program. sti turns interrupts back on, which the BIOS routines need.

A common bug is to skip this block and rely on whatever the BIOS left. It often works in one emulator and fails on real hardware, because the register contents differ.

Printing a character

The loop reads a byte, tests for zero and prints. lodsb loads the byte at ds:si into al and increments si. test al, al sets the zero flag if al is zero, so jz halt ends at the string's terminating zero. For each character, the BIOS teletype function takes ah = 0x0E, the character in al, the page number in bh and a color in bl, which only matters in graphics modes. The call is int 0x10, the video services interrupt. The BIOS prints the character at the cursor, advances the cursor and handles scrolling, and it preserves al, so the loop does not need to reload it.

The string ends with 13, 10, 0: a carriage return, a line feed and the terminator. In text mode, a line feed alone moves down without returning to the left edge, so you need both bytes to start a new line at the left.

Stopping

When the string ends the code must not run off into the zero padding, which would decode as add [bx+si], al forever. cli then hlt stops the CPU until an interrupt, and with interrupts disabled none can arrive, so the jmp halt after it is only a safety loop in case a non-maskable interrupt wakes the CPU.

Check the result

The size and the signature are the two facts to verify before trying to run it:

Shell
ls -l boot.bin
xxd boot.bin | tail -2
Text
-rw-r--r-- 1 user user 512 Nov 23 10:00 boot.bin
000001f0: 0000 0000 0000 0000 0000 0000 0000 55aa  ..............U.

The size must be exactly 512, and the last two bytes must be 55 aa. The user and date in the listing will differ. The start of the file holds your code, and xxd shows it beginning with fa 31 c0, the encodings of cli and xor ax, ax. Disassembling the file confirms the instructions: objdump -D -b binary -m i8086 -M intel --adjust-vma=0x7c00 boot.bin prints the same sequence you wrote.

Why it works on a UEFI machine too

Modern firmware offers a BIOS-compatible mode, often called CSM or legacy boot, that provides the same int 0x10 services. A machine booting purely under UEFI does not run this code, because UEFI loads programs from the EFI System Partition and does not call the real-mode BIOS interrupts. For learning, an emulator running a BIOS image is the usual target, and the post on BIOS and UEFI discusses the choice.

Common mistakes

Forgetting org makes every absolute address wrong by 0x7C00. Writing more than 510 bytes of code fails the build, which is the helpful outcome. Printing with int 0x10 function 0x0E after switching to protected mode fails, because the BIOS interrupt table is a real-mode structure. Using 32-bit registers such as eax in real mode is legal on a 386 and later, but adds an operand-size prefix to each instruction; the code above avoids it to stay on the original instruction set. And omitting the stack setup leaves sp at a value the BIOS chose, which usually works until the first push overwrites something.

Also inKernels
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.