开发者

OS's Boot-loader doesn't work

I am making a custom Operating System. I have two nasm files:

boot.asm:

[BITS 16]   ;tell the assembler that its a 16 bit code
[ORG 0x7C00]    ;Origin, tell the assembler that where the code will
;be in memory after it is been loaded

INT 0x13

JMP $       ;infinite loop

TIMES 510 - ($ - $$) db 0   ;fill the rest of sector with 0
DW 0xAA55           ; add boot signature

start.asm:

[BITS 16]
MOV AL, 72
CALL P开发者_开发百科rintCharacter
MOV AL, 101
CALL PrintCharacter
MOV AL, 108
CALL PrintCharacter
MOV AL, 108
CALL PrintCharacter
MOV AL, 111
CALL PrintCharacter
MOV AL, 44
CALL PrintCharacter
MOV AL, 32
CALL PrintCharacter

MOV AL, 87
CALL PrintCharacter
MOV AL, 111
CALL PrintCharacter
MOV AL, 114
CALL PrintCharacter
MOV AL, 108
CALL PrintCharacter
MOV AL, 100
CALL PrintCharacter
MOV AL, 33
CALL PrintCharacter

PrintCharacter: 
MOV AH, 0x0E
MOV BH, 0x00
MOV BL, 0x07
INT 0x10
RET

TIMES 512 - ($ - $$) db 0

I compile them into .bin files using these commands:

nasm boot.asm -f bin -o boot.bin
nasm start.asm -f bin -o start.bin

Then add them to a floppy image with these commands:

dd if=boot.bin bs=512 of=MyOS.img count=1
dd if=start.bin bs=512 of=MyOS.img count=2

When I boot from the floppy image in VirtualBox it shows 2 exclamation points instead of one and it doesn't even boot in QEmu (Q.app). I am new to Operating System development and so It would be nice if someone could tell me what I did wrong and give me some pointers on how to better set up my OS.


Of course it prints two exclamation marks. Let's look at your code:

...
MOV AL, 33
CALL PrintCharacter    ;   |1
                       ;   |          ^     |4
PrintCharacter:        ;   v    |2    |     |
MOV AH, 0x0E           ;        |     |     |
MOV BH, 0x00           ;        |     |     |
MOV BL, 0x07           ;        |     |     |
INT 0x10               ;        |     |     |     5
RET                    ;        v     |3    v     ----> off to la-la land

Note: I added some arrows that illustrate how program execution proceeds.

The first two lines are responsible for printing the final ! after you've already output Hello, World. This is achieved via a call to your PrintCharacter sub-procedure. (arrows 1 and 2.) When PrintCharacter returns (arrow 3), your program simply continues straight onwards (arrow 4)... and the next line of code happens to be the beginning of PrintCharacter again. Since the AL register still contains 33 (ie. the ANSI code for !), another exclamation mark is printed.

Then, execution once again gets to RET, but this time, since you didn't actually CALL PrintCharacter, there is no defined place to return to, so it returns to... some undefined place, most probably (arrow 5). I suppose that's the instant where your OS stops continuing with the boot process.

Conclusion: After your code prints Hello, World!, it should do something else (it should at least stop), otherwise don't be surprised when you get undefined behaviour or a hang-up...


I don't know how VirtualBox is booting your code, but I'm pretty sure qemu doesn't because you are setting up the binaries on the disk image in the wrong way. When you're using 'dd' on the disk image, you need to pass it an option, so that it doesn't truncate the disk, something like this:

dd if=boot.bin of=MyOS.img bs=512 count=1 conv=notrunc status=noxfer
dd if=start.bin of=MyOS.img bs=512 count=1 conv=notrunc seek=1 status=noxfer

conv=notrunc tells 'dd' that it should not truncate the disk, that is, delete it and overwrite it with your binary, status=noxfer makes it less verbose and seek=1 writes start.bin to the second sector of the disk (starts with 0).

To verify what i'm saying, try creating a disk image of 1MB, and use the command (i.e. dd) you were using and you'll see that the disk image is reduced to a copy of your binary.

So, in the end, you call qemu using start.bin as a disk image (MyOS.img becomes a copy of it after the last 'dd' command), and since you have not used a boot signature at the end of start.bin, the qemu BIOS does not think your disk is bootable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜