OS bootloader not working

I am creating 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 PrintCharacter 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 will compile them into .bin files using the following commands:

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

Then add them to the floppy disk using the following 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 a floppy disk into VirtualBox, it shows 2 exclamation points instead of one, and it doesn't even load into QEmu (Q.app). I am new to developing an operating system and so it would be nice if someone could tell me what I did wrong and give me some tips on how best to configure my OS.

+4
source share
2 answers

Of course, he prints two exclamation points. 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 have added a few arrows that illustrate how the program runs.

The first two lines are responsible for printing the final ! after you have already deduced Hello, World . This is achieved by calling your PrintCharacter routine. (arrows 1 and 2 ) When PrintCharacter returns (arrow 3 ), your program simply continues straight ahead (arrow 4 ) ... and the next line of code becomes the start of PrintCharacter again. Since the AL register still contains 33 (i.e. ANSI code for ! ), Another exclamation point is printed.

Then the execution falls into RET again, but this time, since you are not really a CALL PrintCharacter , there is no specific place to return, so it goes back to ... some undefined place, most likely (arrow 5 ). I believe that at the moment when your OS stops working 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 behavior or freeze, until ...

+5
source

I don’t know how VirtualBox loads your code, but I’m sure that qemu is not because you are creating binaries on a disk image incorrectly. When you use "dd" on a disk image, you need to pass a parameter to it so that it does not cut 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 trim the disk, that is, delete it and overwrite it with your binary file, status = noxfer makes it less verbose and looks for = 1 writes start.bin to the second sector of the disk (starts with 0).

To check what I'm saying, try creating a 1 MB disk image and use the command (i.e. dd) that you used and you will see that the disk image comes down to a copy of your binary file.

So, at the end, you call qemu using start.bin as the disk image (MyOS.img becomes its copy after the last dd command), and since you did not use the initial signature at the end from start.bin, the qemu BIOS does not consider your disk to be bootable .

+1
source

Source: https://habr.com/ru/post/1316233/


All Articles