User:Glauxosdev/Tutorials/Bootloader: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
m (Bootloader != Bootsector)
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
Line 5:
A bootloader is a [[Real Mode|real mode]] program that resides in the first sector of the disk; let it be on a hard disk or on a USB flash disk. The BIOS loads only the bootsector, so to start we have only 512 bytes, unless we load more sectors ourselves. The bootsector is loaded at address 0x00007C00, so using [[Real Mode|real mode]] [[Segmentation|segmentation]] the address is translated to 0x0000:0x7C00 or 0x07C0:0x0000. So for a start we have this code:
 
<sourcesyntaxhighlight lang="asm">
bits 16 ; tell the assembler we want 16 bit code
org 0x7C00 ; tell the assembler to add 0x7C00 offset to labels, segments will be zeroed
Line 30:
db 0x55
db 0xAA
</syntaxhighlight>
</source>
 
Save this file as "boot.asm" and assemble it with nasm:
 
<sourcesyntaxhighlight lang="bash">
nasm -f bin -o boot.bin boot.asm
</syntaxhighlight>
</source>
 
Then copy the resulting binary to the first sector of a USB flash disk, where xxx indicates the block device corresponding to the USB flash disk.
 
<sourcesyntaxhighlight lang="bash">
sudo dd if=boot.bin of=/dev/xxx
</syntaxhighlight>
</source>
 
'''Note:''' Don't try to copy the binary to the hard disk, as it will become unbootable.
Line 52:
Let's add some more code so we can load a sector:
 
<sourcesyntaxhighlight lang="asm">
bits 16 ; tell the assembler we want 16 bit code
org 0x7C00 ; tell the assembler to add 0x7C00 offset to labels, segments will be zeroed
Line 139:
times 512 - ($ - next_sector) db 0
</syntaxhighlight>
</source>
 
If you assemble this file and boot a USB flash disk with the resulting binary, you will see a letter "A" at the top-left corner of the screen, except if your computer is such old so it doesn't support [[BIOS]] [[LBA]] extensions.