Skip to content
systemcoding.dev
Assembly

Test Your Boot Sector in QEMU With qemu-system-x86_64

Boot a 512-byte boot sector in QEMU 9 with a raw drive option, then attach GDB to the emulated CPU to break at 0x7C00 and step real-mode instructions.

6 min read
Summarize in AI
Flat illustration of guest machines running on a hypervisor over hardware, with the QEMU, Intel and Linux logos.

A boot sector can be tested on real hardware, but the loop of writing a USB stick, rebooting and watching a blank screen is slow, and a mistake gives you no information. An emulator replaces that loop with one command. QEMU emulates a PC with a BIOS, a disk and a display, boots whatever sector you give it and lets you attach a debugger to its virtual CPU, so you can stop at the very first instruction at 0x7C00 and step through. This post uses QEMU 9 and GDB 15 on Linux to run the boot.bin built in the previous post, explains the options that matter, and shows how to debug 16-bit real mode code, which GDB needs a few special settings to handle. The commands only run the emulator; nothing here changes your machine.

Boot it

The minimal command points the emulator at the file as a raw disk image:

Shell
qemu-system-x86_64 -drive format=raw,file=boot.bin

QEMU opens a window, runs its built-in BIOS (SeaBIOS by default) and boots from the drive. The window shows SeaBIOS messages and then your text. Two options do real work. format=raw tells QEMU not to guess the file format. Without it, QEMU probes the file, and a file that looks like a raw image works, but newer QEMU versions warn that probing a raw image is a security risk and may refuse writes to the first sector. Always state format=raw. file=boot.bin is your 512-byte image. A disk image that is only 512 bytes is a valid, tiny disk: QEMU pads access beyond the end with zeros.

For a floppy-style boot, the shortcut -fda boot.bin attaches the file as the first floppy drive, and the BIOS boots from it. A floppy has a fixed geometry, and QEMU rounds the size up, which does not matter for a sector that only reads itself.

Text-mode output in a terminal

Run without a window when you only need the text:

Shell
qemu-system-x86_64 -drive format=raw,file=boot.bin -display curses

-display curses draws the VGA text screen in your terminal, which suits a boot sector that only prints text. Quit with Esc then 2 and q in the monitor, or close the terminal. The -nographic option redirects the serial port and monitor to the terminal and turns off the window, but BIOS int 0x10 output goes to the VGA screen, not to the serial port, so with -nographic you will see nothing from the sample unless the program writes to the serial port itself. This is the most common source of "my boot sector prints nothing" confusion.

Get out when it hangs

A boot sector that ends with cli; hlt leaves the emulator running with a halted CPU, and the window stays open. Close the window or press Ctrl-C in the terminal. Add -no-reboot so a triple fault, which would otherwise make the virtual PC reset and loop forever, stops the emulator instead:

Shell
qemu-system-x86_64 -drive format=raw,file=boot.bin -no-reboot -d int,cpu_reset -D qemu.log

-d int,cpu_reset logs every interrupt and every CPU reset, and -D qemu.log sends the log to a file. If your code crashes, the log shows the exception and the register state, which turns "it rebooted" into a cause.

Debug it with GDB

QEMU has a built-in GDB server. Start the emulator paused and listening:

Shell
qemu-system-x86_64 -drive format=raw,file=boot.bin -S -gdb tcp::1234

-S freezes the CPU before the first instruction, and -gdb tcp::1234 opens the server on port 1234 (the shorthand -s is the same as -gdb tcp::1234). Nothing happens in the window until a debugger connects and continues. In another terminal:

Text
$ gdb
(gdb) set architecture i8086
(gdb) target remote localhost:1234
(gdb) break *0x7c00
(gdb) continue

set architecture i8086 tells GDB to decode 16-bit code. The default for an x86-64 GDB is 64-bit, and the disassembly of real mode bytes in that mode is garbage. The break uses *0x7c00, a raw address, since there are no symbols. When the breakpoint hits, the CPU is at the first byte of your sector.

Reading instructions in real mode

Real mode addresses are segment * 16 + offset, and GDB shows the offset only, in $pc or $eip. The instruction at the current location is at the physical address $cs * 16 + $eip. To print the next few instructions, use that expression:

Text
(gdb) x/5i $cs*16 + $eip
=> 0x7c00:	cli
   0x7c01:	xor    %ax,%ax
   0x7c03:	mov    %ax,%ds
   0x7c05:	mov    %ax,%es
   0x7c07:	mov    %ax,%ss

The output uses AT&T syntax; switch with set disassembly-flavor intel. Step with stepi and print registers with info registers. A bug that is easy to see this way is a wrong segment register: if ds holds 0x7C0 instead of 0 after your setup, then x/s at your message address reads the wrong place.

A caveat: some GDB versions show the first instruction after a mode switch incorrectly, because the debugger's idea of the mode is fixed by set architecture. When your code switches from real mode to protected mode, set architecture i386 at that point to read the 32-bit code that follows.

Inspect memory and the screen

The text screen is memory at 0xB8000, two bytes per cell: the character and its attribute. After your program prints, examine it from GDB:

Text
(gdb) x/16xb 0xb8000

The first bytes show 48 07 65 07 6c 07 ... for "Hel" with the light-gray attribute 0x07, if you wrote directly to the screen. Output through int 0x10 goes to the same place. QEMU's own monitor, available with -monitor stdio, adds commands such as info registers and xp /16xb 0xb8000 that read guest physical memory without a debugger attached.

Useful options in one place

OptionEffect
-drive format=raw,file=boot.binBoot from a raw disk image
-fda boot.binAttach as a floppy
-display cursesShow the text screen in the terminal
-S -gdb tcp::1234Pause at reset and open a GDB server
-no-rebootExit on a reset instead of looping
-d int,cpu_reset -D fileLog interrupts and resets
-monitor stdioQEMU's monitor on the terminal
-cpu qemu64 or -cpu hostChoose the virtual CPU

For a reproducible setup, put the command in a Makefile target so that a build and a run are one step. Also keep the 512-byte check in the build, since an image of the wrong size or without 55 AA boots to the message "No bootable device" and not to your code.

What QEMU does not tell you

QEMU is not a real PC. Its BIOS is SeaBIOS, and real firmware differs in details: which registers hold what at 0x7C00, how fast the disk responds, whether the A20 line starts enabled. Code that works in QEMU can fail on hardware, and the reverse is rare. So test the assumptions that matter, such as the boot drive number in dl and the real-mode segments, instead of leaning on what the emulator happens to do, and run the finished image on a real machine at least once. The difference between a virtual and a real floppy or USB device is also visible: QEMU's -fda gives you geometry int 0x13 is happy with, while a USB stick presented as a hard disk follows different rules.

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.