Babystep1: Difference between revisions

m
no edit summary
[unchecked revision][unchecked revision]
(Add to Category Bootloaders)
mNo edit summary
 
(16 intermediate revisions by 12 users not shown)
Line 1:
__NOTOC__
 
{{Tone}}
{{Rating|1}}
 
Line 7 ⟶ 8:
| next=[[Babystep2]]
}}
 
=== Your first boot sector. ===
 
==Code==
The following code is the smallest possible example of booting code from a floppydisk.
 
<sourcesyntaxhighlight lang="asm">
; boot.asm
hang:
Line 16 ⟶ 20:
 
times 512-($-$$) db 0
</syntaxhighlight>
</source>
The CPU starts in real mode and the [[BIOS]] loads this code at address 0000:7c00. The "<code>times 512..."-($-$$) stuffdb 0</code> is NASM's way of saying fill up 512 bytes with zeros.zeroes, Andand partcopy is going to expect that (200 in Hex = 512 in Decimal). Change it and you'llpartcopy seewill Partcopylikely chokefail.
 
Often,There youis will seeoften a so-called boot signature (0xAA55) at the end. Older versions of [[BIOS|some BIOSes]] looked for this in order to identify a boot sector on a disk. It is evidentiallyevidently unnecessary nowadays, unless you're running the code on a legacy BIOS, or in QEMU. If it's needed, the last line would be replaced with (or some version of it):
<sourcesyntaxhighlight lang="asm">
; boot.asm
hang:
Line 28 ⟶ 32:
db 0x55
db 0xAA
</syntaxhighlight>
</source>
 
But the thing I'd really like to point out is how onceOnce you've booted, and the cursor is happily blinking on a blank screen, you might notice two things. One is that the floppydisk's motor will turn off and the other is that you can now press Ctrl-Alt-Del to reboot. The pointThis is thatbecause [[interrupts]] (such as INT 0x09) asare still being generated.
 
For kicks tryTry clearing the interrupts flag:
<sourcesyntaxhighlight lang="asm">
;boot.asm
cli
Line 42 ⟶ 46:
db 0x55
db 0xAA
</syntaxhighlight>
</source>
You may notice that the floppy motor doesn't turn off and you can't reboot with Ctrl-Alt-Del.
 
If you try to reduce this even more by removingRemoving the loop and merely padpadding out the sector with zeros,zeroes will usually cause the BIOS willto havethrow somethingan toerror sayon about itboot. On mymost machinemachines, it waswill say "Operating System Not Found". I have yet to try filling the sector with zeros except for adding a boot signature.
 
Not exactly something you would show your girlfriend, but I wanted to show just what the bare minimum is before I elaborate. Unless I'm irritating anyone, in which case I'll desist.
 
==Creating disk image==
The code is assembled in [[NASM]] and copied to floppy using(outdated), eitherdisk or USB-sticks using partcopy, dd, or dddebug. Then you simply boot from thethat floppydisk.
 
For a more detailed description, see the [[Bootable Disk]] page.
 
===Windows===
<sourcesyntaxhighlight lang="bash">
nasmw boot.asm -f bin -o boot.bin
partcopy boot.bin 0 200 -f0
OR
</source>
debug boot.bin
-W 100 0 0 1
-Q</syntaxhighlight>
===Unix===
<sourcesyntaxhighlight lang="bash">
nasm boot.asm -f bin -o boot.bin
dd if=boot.bin of=/dev/fd0
</syntaxhighlight>
</source>
To write to a hard drive or USB-stick, use
<syntaxhighlight lang="bash">
nasm boot.asm -f bin -o boot.bin
dd if=boot.bin of=/dev/sda
</syntaxhighlight>
where replace "sda" with sdb, sdc etc. according to your configuration.
 
==Running the binary in QEMU==
If you don't have an old machine with floppy drive you can emulate one using QEMU (note "'''f'''da").
 
<syntaxhighlight lang="bash">
qemu-system-i386 -fda boot.bin
</syntaxhighlight>
 
But it is advisable to forget about floppies altogether, and focus on USB-sticks instead. Also if you're afraid to test your code on your development machine (that would be wise), you can use QEMU (note "'''h'''da").
 
<syntaxhighlight lang="bash">
qemu-system-i386 -hda boot.bin
</syntaxhighlight>
 
Use the QEMU monitor command to send Ctrl-Alt-Del to the VM:
<syntaxhighlight lang="bash">
sendkey ctrl-alt-delete
</syntaxhighlight>
 
Because of how fast emulation has become, you might need to slow down emulation speed to 1% to notice the reboots.
 
== References ==
Line 70 ⟶ 103:
* Interrupts by number: [http://www.osdever.net/downloads.php]
* Randall Hyde's look into the bowels of the PC: [http://webster.cs.ucr.edu/]
* QEMU [https://www.qemu.org]
 
[[Category:Babystep]]