4

Currently, I am trying to write a tiny OS from scratch. Unfortunately, In the first step, I faced a problem which drives me crazy. I write the following code as my bootloader.

.code16                   #generate 16-bit code
.text                     #executable code location
     .globl _start;
_start:                   #code entry point
     . = _start + 510     #mov to 510th byte from 0 pos
     .byte 0x55           #append boot signature
     .byte 0xaa           #append boot signature

In next step, I compile it with following commands:

as bootloader.s -o bootloader.o
ld –Ttext 0x7c00 --oformat=binary bootloader.o –o bootloader.bin

Then I create an img file with the following commands:

dd if=/dev/zero of=boot.img bs=512 count=2880
dd if=bootloader.bin of=boot.img

Finally, when I run bochs command in the terminal, Bochs just show me a dark window and nothing appeared. What is wrong?

Also, I tried the following code as bootloader but the result is same as previous.

.code16                  #generate 16-bit code
.text                    #executable code location
     .globl _start;

_start:                  #code entry point

     #print letter 'H' onto the screen
     movb $'H' , %al
     movb $0x0e, %ah
     int  $0x10

     #print letter 'e' onto the screen
     movb $'e' , %al
     movb $0x0e, %ah
     int  $0x10

     #print letter 'l' onto the screen
     movb $'l' , %al
     movb $0x0e, %ah
     int  $0x10

     #print letter 'l' onto the screen
     movb $'l' , %al
     movb $0x0e, %ah
     int  $0x10

     #print letter 'o' onto the screen
     movb $'o' , %al
     movb $0x0e, %ah
     int  $0x10

     #print letter ',' onto the screen
     movb $',' , %al
     movb $0x0e, %ah
     int  $0x10

     #print space onto the screen
     movb $' ' , %al
     movb $0x0e, %ah
     int  $0x10

     #print letter 'W' onto the screen
     movb $'W' , %al
     movb $0x0e, %ah
     int  $0x10

     #print letter 'o' onto the screen
     movb $'o' , %al
     movb $0x0e, %ah
     int  $0x10

     #print letter 'r' onto the screen
     movb $'r' , %al
     movb $0x0e, %ah
     int  $0x10

     #print letter 'l' onto the screen
     movb $'l' , %al
     movb $0x0e, %ah
     int  $0x10

     #print letter 'd' onto the screen
     movb $'d' , %al
     movb $0x0e, %ah
     int  $0x10

     . = _start + 510      #mov to 510th byte from 0 pos
     .byte 0x55            #append boot signature
     .byte 0xaa            #append boot signature
Adonaim
  • 170
  • 1
  • 2
  • 11
  • 1
    You should at least do something like `jmp .` at the end of the code to get into an infinite loop otherwise you'll be executing whatever happens to be in memory after that last `int $0x10` – Michael Petch Sep 26 '16 at 21:37
  • Preferably I'd do `endloop: hlt` `jmp endloop` instead of my simpler solution of `jmp .` in the first comment. Either one can be placed at the end of your code. – Michael Petch Sep 26 '16 at 21:46
  • hmmm nothing appeared again. bochs just show a dark screen. :( – Adonaim Sep 26 '16 at 21:49
  • I've tried your code with my fix here on Bochs and it works as expected. The second example prints `Hello World` when I place `jmp .` right after the last `int $0x10` and before the boot signature.. Where did you place the `jmp .`? The first example you gave even with `jmp .` won't do a thing since it did print anything noticeable on the screen. The only other possibility is your Bochs config is messed up. – Michael Petch Sep 26 '16 at 21:50
  • And by dark window, if you mean absolutely nothing printed in it then is there a debug window present at a prompt similar to `` ? If so you may have to use the command `c` in the debugger to actually start executing the code. `c` is short for `continue` – Michael Petch Sep 26 '16 at 21:55
  • Now bochs give me the error : Could not read the boot disk – Adonaim Sep 26 '16 at 21:56
  • I add it and then I compile it again. But bochs give me the error. – Adonaim Sep 26 '16 at 22:00
  • Add to your question your bochs configuration file. – Michael Petch Sep 26 '16 at 22:00
  • I used ubuntu. Can you tell me how you create img file? I think this file has problem. I used dd command but the file has 512 mb. In the tutorial it must have 1 mb file. – Adonaim Sep 26 '16 at 22:01
  • How can I find out bochs config file? – Adonaim Sep 26 '16 at 22:04
  • You can build the disk image with `dd if=/dev/zero of=boot.img bs=512 count=2880` followed by `dd if=bootloader.bin of=boot.img conv=notrunc` . `conv=notrunc` prevents the disk image file from being truncated. – Michael Petch Sep 26 '16 at 22:05
  • If you don't have a bochs config fie then let us see the command line you use to run Bochs. – Michael Petch Sep 26 '16 at 22:06
  • as bootloader.s -o bootloader.o ld -Ttext 0x7c00 --oformat=binary bootloader.o -o bootloader.bin dd if=/dev/zero of=boot.img bs=512 count=2880 2880+0 records in 2880+0 records out 1474560 bytes (1.5 MB, 1.4 MiB) copied, 0.0108683 s, 136 MB/s dd if=bootloader.bin of=boot.img conv=notrunc 1+0 records in 1+0 records out 512 bytes copied, 0.000184225 s, 2.8 MB/s bochs – Adonaim Sep 26 '16 at 22:09
  • I run simply bochs and then 6 number to begin simulation. But nothing appeared. – Adonaim Sep 26 '16 at 22:10
  • Do you have a file in your directory called bochsrc.txt? – Michael Petch Sep 26 '16 at 22:10
  • No i dont have this file. – Adonaim Sep 26 '16 at 22:11
  • Okay well that is your problem. You need to supply command line parameters or create a bochsrc.txt file that has a floppy disk that points at boot.img. The reason you have no bootable disk is because you haven't told Bochs where to boot from or what disk image file (`boot.img`) to use. – Michael Petch Sep 26 '16 at 22:12
  • Hmmm is that possible I use something else rather floppy? – Adonaim Sep 26 '16 at 22:13
  • You can give a bochsrc.txt code sample? – Adonaim Sep 26 '16 at 22:16
  • I'll give you an example command line for bochs that doesn't require a bochsrc.txt. Try this: `bochs -q 'boot:a' 'floppya: 1_44=boot.img, status=inserted'` . That should tell Bochs that you will be booting from Drive A and that it is a 1.44MB floppy and the contents will be in the fie `boot.img` and will start out as _inserted_. – Michael Petch Sep 26 '16 at 22:17
  • 2
    haa thanksss broo. now everything works fine.. – Adonaim Sep 26 '16 at 22:23
  • You need to create a bochsrc.txt file. For more info look here : http://bochs.sourceforge.net/doc/docbook/user/bochsrc.txt. If you are in Windows you can directly download my bootloader and see the bochsrc.txt. Your self : https://github.com/amanuel2/OS_Mirror_Win – amanuel2 Sep 29 '16 at 01:51

1 Answers1

0

You must create a bochs file in order it works well. Also You can use virtual machine. I test your code and it works fine.