7

I recently had an idea to start developing my own Operating System. After reading many articles on different sites that I thought would help me with this task, I thought I could start now. (I am using Ubuntu 14.10 x64 by the way)

Since a floppy disk is the simplest storage medium for developing OSes, I acquired a 3.5 inch floppy disk drive.

I am using NASM as an assembly compiler, and qemu as an emulator. Using the dd command, I cloned an existing and empty (in terms of files) floppy disk to a file called floppy.img.bak .

After that, I wrote a simple bootloader in x86 assembly:

bootloader.asm

org 7C00h
jmp 0x0000:start    ;go 

msg db 'Loading Kernel...', 0

start:
    ;update the segment registers
    mov ax, cs
    mov ds, ax
    mov es, ax

    mov si, msg

print:          ;prints a string
    lodsb       ;load next char

    cmp al, 0   ;if null terminator...
    je reset    ;...jump to reset:

    mov ah, 0Eh ;print AL
    mov bx, 7   
    int 10h

    jmp print   ;if not null terminator, continue printing

reset:          ;resets the floppy drive
    mov ax, 0   ;
    mov dl, 0   ;drive=0 (=A)
    int 13h     ;
    jc reset    ;if error resetting, reset again

read:
    mov ax, 1000h ;ES:BX = 1000:000
    mov es, ax  ;es is 1000h now
    mov bx, 0   ;bx is 0 now

    mov ah, 2   ;load disk data into ES:BX
    mov al, 1   ;load 1 sector
    mov ch, 0   ;cylinder=0
    mov cl, 2   ;sector=2
    mov dh, 0   ;head=0
    mov dl, 0   ;drive=0
    int 13h     ;read!

    jc read     ;if error then try again


    jmp 1000h:0000;jump to the program

times 510-($-$$) db 0
dw 0AA55h

So far so good. My simple temporary stub kernel is as follows:

kernel.asm

kstart:
    mov ah, 9
    mov al, 'k'
    mov bx, 7
    mov cx, 5
    int 10h

hang:
    jmp hang

times 510-($-$$)+2 db 0

I also have a shell script to compile, write and boot this setup:

compile-and-run.sh

cd ~/Dev/OS                                                          # cd to here

rm asm-bin/bootloader.bin                                               # remove old compiled bootloader
rm asm-bin/kernel.bin                                                   # remove old compiled kernel

nasm asm-src/bootloader.asm -f bin -o asm-bin/bootloader.bin            # compile bootloader
nasm asm-src/kernel.asm -f bin -o asm-bin/kernel.bin                    # compile kernel

rm images/floppy.img                                                    # remove old floppy image
cp images/floppy.img.bak images/floppy.img                              # copy original floppy image to fresh one

dd if=asm-bin/bootloader.bin of=images/floppy.img bs=512 count=1 seek=0 # write bootloader to first sector
dd if=asm-bin/kernel.bin of=images/floppy.img bs=512 count=1 seek=1     # write kernel to second sector

qemu-system-i386 images/floppy.img                                      # start qemu and boot floppy.img

Now, the expected output in qemu would be (at least what I understand):

Loading Kernel...
kkkkk

But instead, it is:

Loading Kernel...

So, obviously, there is something wrong at the jump, I just don't know what. Maybe you could help me? I would appreciate it.

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
  • 2
    I'd have your bootstrap code print out a letter for each step in the process so you have a better idea where to look. – Ross Ridge Oct 25 '14 at 22:48

2 Answers2

3

The bootdrive is stored in dl register but you override it to 0.
By default the bootdrive is 80h and not 0 as used by the bootloader code.
If you comment out the 2 lines

;    mov dl, 0   ;drive=0 (=A)

It will boot as you expect.

nrz
  • 10,435
  • 4
  • 39
  • 71
mpromonet
  • 11,326
  • 43
  • 62
  • 91
  • You're welcome ! This is just an easy way to make work your code, however if you make a more complex bootloader, you will need to store dl (like in http://caca.zoy.org/browser/libcaca/trunk/kernel/boot/bootsect.asm) – mpromonet Oct 26 '14 at 12:00
2

Fix your script, you need to run it with qemu-system-i386 -fda images/floppy.img. Notice the -fda parameter. If you omit that, your image will be attached as hard disk and not as floppy disk, so your read will fail (since you have hard-coded the floppy drive).

Jester
  • 56,577
  • 4
  • 81
  • 125