baby steps #1 - boot loop

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
crazybuddha

baby steps #1 - boot loop

Post by crazybuddha »

This is meant as a public service :)

The following code is the smallest possible example of booting code from a floppy. It is assembled in NASM and copied to floppy using Partcopy. Then you boot from the floppy.

;---------------- CUT ME -------------------
; nasmw boot.asm -f bin -o boot.bin
; partcopy boot.bin 0 200 -f0

hang:
???jmp hang
?????????
???times 512-($-$$) db 0
;---------------- CUT ME -------------------

The CPU starts in real mode and BIOS loads this code at 0000:7c00. The "times 512..." stuff is NASM's way of saying fill up 512 bytes with zeros. And Partcopy is going to expect that (200 in hex = 512). Change it, and you'll see Partcopy choke.

Often, you will see a so-called boot signature (0xAA55) at the end. Older versions of BIOSes looked for this in order to identify a boot sector on a disk. It is evidentally unnecessary nowadays. If it's needed, the last line would be replaced with (or some version of it)

;------------------------------------
???times 510-($-$$) db 0 ;2 bytes less now
???dw 0xAA55
;------------------------------------

But the thing I'd really like to point out is how once you've booted, and the cursor is happily blinking on a blank screen, you might notice two things. One is that the floppy motor will turn off and the other is that you can press Crtl-Alt-Del to reboot. The point is that interrupts ( such as int 0x09) are still being generated.

For kicks try clearing the interrupts flag:
;--------------------------------------------
???cli
hang:
???jmp hang
?????????
???times 512-($-$$) db 0
;--------------------------------------------

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 removing the loop and merely pad out the sector with zeros, the BIOS will have something to say about it. On my machine, it was "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 that the bare minimum is just that before I elaborate. Unless I'm irritating anyone, in which case I'll desist.




-------------------------------------
REFERENCES:
*** Instruction Set from the horse's mouth:
http://www.intel.com/design/PentiumII/manuals/

*** Easier to read
http://www.baldwin.cx/386htm/toc.htm

*** NASM assembler - docs incl Instruction Set
http://nasm.2y.net/

*** Partcopy - download pcopy02.zip
http://www.execpc.com/~geezer/johnfine/

*** Interrupts by number
http://www.ctyme.com/intr/int.htm


*** Randall Hyde's look into the bowels of the PC
http://webster.cs.ucr.edu/
K.J.

Re:baby steps #1 - boot loop

Post by K.J. »

Or possibly even smaller:

[tt]
[ORG 0x7c00]
jmp $

times 510-($-$$) db 0
dw 0xAA55
[/tt]

Without 0xAA55 at the end of the bootsector, most PCs won't load and run a bootsector.

K.J.
crazybuddha

Re:baby steps #1 - boot loop

Post by crazybuddha »

I will incorporate both suggestions in future installments. Thanks.
Federico Tomassetti

Re:baby steps #1 - boot loop

Post by Federico Tomassetti »

Nothing to say about multiboot standard?
I think it's a very good way to start an OS
because it's so possible avoid some boring
thing like A20 and focus on "real" OS code.
Post Reply