Monday, October 15, 2012

Simple 8086 Assembly Bootloader

Using an assembler such as NASM, the following code could be used to create a bootloader for a PC:
It can be compiled into a floppy image through:
nasm filename.asm -f bin -o filename.img


[BITS 16] ;This code is 16-bit
[ORG 0x7C00] ;The code will initially be loaded at address 0x7C00 in RAM, the              ;location where the PC initially looks for executable code

main:
jmp $ ;Infinite Loop   

times 510-($-$$) db 0 ;The preceding code does not take up 512 bytes(a sector)                      ;this line tells the assembler to pad (510-CodeSize)                          ;bytes
dw 0xAA55 ;The hexidecimal bytes 0xAA & 0x55 are required in the last two bytes of the 512 byte sector in order for the BIOS to recognize this code as a bootloader.