Babystep1: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
(Added rating.)
mNo edit summary
Line 10: Line 10:
The following code is the smallest possible example of booting code from a floppy.
The following code is the smallest possible example of booting code from a floppy.


<source lang="asm">
<pre>
; boot.asm
; boot.asm
hang:
hang:
Line 16: Line 16:


times 512-($-$$) db 0
times 512-($-$$) db 0
</pre>
</source>
The CPU starts in real mode and the BIOS loads this code at address 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 in Decimal). Change it and you'll see Partcopy choke.
The CPU starts in real mode and the BIOS loads this code at address 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 in Decimal). Change it and you'll see Partcopy choke.


Often, you will see a so-called boot signature (0xAA55) at the end. Older versions of [[BIOS|BIOSes]] looked for this in order to identify a boot sector on a disk. It is evidentially unnecessary nowadays. If it's needed, the last line would be replaced with (or some version of it)
Often, you will see a so-called boot signature (0xAA55) at the end. Older versions of [[BIOS|BIOSes]] looked for this in order to identify a boot sector on a disk. It is evidentially unnecessary nowadays. If it's needed, the last line would be replaced with (or some version of it)
<source lang="asm">
<pre>
; boot.asm
; boot.asm
hang:
hang:
Line 28: Line 28:
db 0x55
db 0x55
db 0xAA
db 0xAA
</pre>
</source>


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 Ctrl-Alt-Del to reboot. The point is that interrupts (such as INT 0x09) as still being generated.
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 Ctrl-Alt-Del to reboot. The point is that interrupts (such as INT 0x09) as still being generated.


For kicks try clearing the interrupts flag:
For kicks try clearing the interrupts flag:
<source lang="asm">
<pre>
;boot.asm
;boot.asm
cli
cli
Line 42: Line 42:
db 0x55
db 0x55
db 0xAA
db 0xAA
</pre>
</source>
You may notice that the floppy motor doesn't turn off and you can't reboot with Ctrl-Alt-Del.
You may notice that the floppy motor doesn't turn off and you can't reboot with Ctrl-Alt-Del.


Line 53: Line 53:


===Windows===
===Windows===
<source lang="bash">
nasmw boot.asm -f bin -o boot.bin
partcopy boot.bin 0 200 -f0
nasmw boot.asm -f bin -o boot.bin
partcopy boot.bin 0 200 -f0
</source>
===Unix===
===Unix===
<source lang="bash">
$ nasm boot.asm -f bin -o boot.bin
$ dd if=boot.bin of=/dev/fd0
nasm boot.asm -f bin -o boot.bin
dd if=boot.bin of=/dev/fd0
</source>


== References ==
== References ==

Revision as of 01:32, 10 May 2009

Difficulty level

Beginner


Babystep1: Your first boot sector

Tutorial

PreviousNext
Babystep2

Code

The following code is the smallest possible example of booting code from a floppy.

; boot.asm
hang:
    jmp hang

    times 512-($-$$) db 0

The CPU starts in real mode and the BIOS loads this code at address 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 in Decimal). 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 evidentially unnecessary nowadays. If it's needed, the last line would be replaced with (or some version of it)

; boot.asm
hang:
    jmp hang

    times 510-($-$$) db 0 ; 2 bytes less now
    db 0x55
    db 0xAA

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 Ctrl-Alt-Del to reboot. The point is that interrupts (such as INT 0x09) as still being generated.

For kicks try clearing the interrupts flag:

;boot.asm
     cli
 hang:
     jmp hang

     times 510-($-$$) db 0
     db 0x55
     db 0xAA

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 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 either partcopy or dd. Then you simply boot from the floppy.

Windows

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

Unix

nasm boot.asm -f bin -o boot.bin
dd if=boot.bin of=/dev/fd0

References

  • Instruction Set from the horse's mouth: [1]
  • Easier to read: [2]
  • NASM asembler - docs incl instruction set: [3]
  • Partcopy - download pcopy02.zip (new link): [4]
  • Interrupts by number: [5]
  • Randall Hyde's look into the bowels of the PC: [6]