Boot sector: hello in 512 bytes
The setup
A boot sector is 512 raw bytes the BIOS loads at physical address 0x7C00 and jumps to. Assemble with -f bin, set the origin, and switch to 16-bit mode, because the CPU starts in real mode:
bits 16 org 0x7C00 start: ; real-mode code here
Printing in real mode
In real mode there are no syscalls: the BIOS provides services through INT 10h. Function 0Eh of INT 10h prints one character in AL to the screen, teletype style:
mov si, msg print_loop: lodsb ; AL = [SI], SI++ or al, al jz done mov ah, 0x0E int 0x10 jmp print_loop done: jmp $ ; halt here msg db "Hello from a boot sector", 0
The signature
The last two bytes must be 0x55 then 0xAA, or the BIOS rejects the disk. Pad to 510 bytes and append the signature with TIMES:
times 510-($-$$) db 0 dw 0xAA55
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
At which physical address does the BIOS load a boot sector?
Answer
0x7C00
The BIOS reads the first sector to 0x7C00 and jumps to it.
-
How large is a boot sector?
Answer
512 bytes
One disk sector is 512 bytes, and the signature sits in the last two.
-
In which CPU mode does the BIOS start a boot sector?
Answer
16-bit real mode
The CPU starts in real mode, so the sector begins with
bits 16. -
What does
org 0x7C00tell the assembler?Answer
The address where the code will be loaded
The flat binary has no header, so the origin makes the labels resolve.
-
Which BIOS service prints one character in real mode?
Answer
INT 10h, function 0Eh
Function 0Eh of the video service prints AL in teletype style.
-
Which register holds the character for INT 10h function 0Eh?
Answer
AL
AH selects the function and AL carries the character.
-
What does
lodsbdo?Answer
It loads a byte from [SI] into AL and increments SI
The string instruction walks the message one byte at a time.
-
Which two bytes end a valid boot sector?
Answer
0x55 then 0xAA
dw 0xAA55writes 55 AA in file order, and the BIOS checks for it. -
What does
times 510-($-$$) db 0do?Answer
It pads the file to 510 bytes with zeros
The expression is the space left before the signature, and
$and$$are the current and section start addresses. -
Which NASM format writes the boot sector?
Answer
-f bin
A boot sector is raw bytes with no headers, which is the bin format.
The code on this page was not run: a boot sector needs a PC that boots from it. The three snippets were assembled together with NASM 3.01 as a 512-byte file ending in 0xAA55; they are fragments of one sector, not separate programs.
Article author: Arthur Isaev