ali
ali

Reputation: 11045

Assembly - Read next sector of a virtual disk

As any programmer in the world at least once in his/her life, I am trying to create my "revolutionary", the new and only one operating system. :D

Well, I am using a virtual emulator (Oracle VM Virtual Box), for which I create a new unknwon operating system, with a vmdk disk. I like vmdk because they are just plain files, so I can paste my boot-loader over the first 512 bytes of the virtual hard disk.

Now, I am trying to read the next sector of this virtual disk, on which I would paste a simple kernel that would display a message.

I have two questions:

So, this is the way I am trying to add the kernel to the second sector. What do you think is wrong with this? Thanks!

UPDATE

O.K. I don't get any error now, but I don't see the loaded code being executed. It should display a point on the window:

;--------------------------------------------
; 'load.asm'
; loaded from 'boot.asm'

[org 0x8000]
[bits 16]

;--------------------------------------------

main:
mov ah, 0x0E  ; print function
mov al, '.'   ; ascii char
int 0x10   ; IO int

jmp $    ; hang

Upvotes: 4

Views: 3437

Answers (2)

Alexey Frunze
Alexey Frunze

Reputation: 62048

One problem is here:

jmp [es:bx]

This will read an address, a 16-bit offset, in fact, from the memory location at the address contained in the registers es (segment portion) and bx (offset portion) and then set ip to that 16-bit offset.

What you may want to use instead is:

jmp some_constant1:some_constant2

This will set cs to some_constant1 and ip to some_constant2. Unsurprisingly, good candidates for these two constants are 0x8000 and 0 respectively since that's the location where your code gets loaded.

Now, the second problem is here:

[org 0x8000]

This org tells NASM to generate code in such a way that it will work if loaded at offset 0x8000. Now, offset 0x8000 is not the same thing as segment 0x8000. If you use jmp 0x8000:0, then you should also use:

[org 0]

Upvotes: 6

ughoavgfhw
ughoavgfhw

Reputation: 39905

Try again after it fails. I think you will receive an error indication with no message when the disk is spinning up, so the emulator may fail the first time on purpose. Four attempts has worked well for me in both bochs and qemu, but I have not tried it on anything else. You may also want to reset the drive controller before reading to clear any previous errors. Use interrupt 0x13 with al clear and the drive number in dl.


Note: Hardcoding the drive number may work for now, but does not let you support booting from other drives. The BIOS should leave the drive number in dl when starting your bootloader, so you can save that.

Upvotes: 1

Related Questions