2

I wrote some codes to be written to boot sector and display an 'L'.

mov ax,0xb800
mov es,ax
mov byte [es:0x00],'L'
mov byte [es:0x01],0x07

The segment 0xb800 is for video memory,so I put 'L' into it and 'L' will be displayed.

It works well if I use VirtualBox but noting displayed if bochs.

I use nasm -f bin boot.asm -o boot.bin to build the bootloader.

  1. I run on windows 7 and windows 8,it is OK.

-I create a VHD disk image then I build the codes and write it to the disk image,after that I start in a VirtualBox and it worked.

2.I run in a Ubuntu 14.04 i386 virtual machine with Bochs,it does not work. my bochs setting:(Bochs x86 Emulator 2.4.6)

megs:32
romimage: file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage:file=/usr/share/bochs/VGABIOS-lgpl-latest
floppya:1_44=floppy.img,status=inserted
boot:a
mouse:enabled=0
display_library:sdl

and I build the codes with NASM and write it to floppy.img with dd if=boot.bin of=floppy.img. After start Bochs,nothing displayed but the cursor fast blink.

These my complete codes:

         mov ax,0xb800                 
         mov es,ax

         ;just display string "Label offset:"
         mov byte [es:0x00],'L'
         mov byte [es:0x01],0x07
         mov byte [es:0x02],'a'
         mov byte [es:0x03],0x07
         mov byte [es:0x04],'b'
         mov byte [es:0x05],0x07
         mov byte [es:0x06],'e'
         mov byte [es:0x07],0x07
         mov byte [es:0x08],'l'
         mov byte [es:0x09],0x07
         mov byte [es:0x0a],' '
         mov byte [es:0x0b],0x07
         mov byte [es:0x0c],"o"
         mov byte [es:0x0d],0x07
         mov byte [es:0x0e],'f'
         mov byte [es:0x0f],0x07
         mov byte [es:0x10],'f'
         mov byte [es:0x11],0x07
         mov byte [es:0x12],'s'
         mov byte [es:0x13],0x07
         mov byte [es:0x14],'e'
         mov byte [es:0x15],0x07
         mov byte [es:0x16],'t'
         mov byte [es:0x17],0x07
         mov byte [es:0x18],':'
         mov byte [es:0x19],0x07

         mov ax,number                 
         mov bx,10

         ;set base address to ds
         mov cx,cs
         mov ds,cx

         ;below codes just display the offset of label "number" in decimal base
         mov dx,0
         div bx
         mov [0x7c00+number+0x00],dl   

         xor dx,dx
         div bx
         mov [0x7c00+number+0x01],dl   

         xor dx,dx
         div bx
         mov [0x7c00+number+0x02],dl   

         xor dx,dx
         div bx
         mov [0x7c00+number+0x03],dl   

         xor dx,dx
         div bx
         mov [0x7c00+number+0x04],dl   

         mov al,[0x7c00+number+0x04]
         add al,0x30
         mov [es:0x1a],al
         mov byte [es:0x1b],0x04

         mov al,[0x7c00+number+0x03]
         add al,0x30
         mov [es:0x1c],al
         mov byte [es:0x1d],0x04

         mov al,[0x7c00+number+0x02]
         add al,0x30
         mov [es:0x1e],al
         mov byte [es:0x1f],0x04

         mov al,[0x7c00+number+0x01]
         add al,0x30
         mov [es:0x20],al
         mov byte [es:0x21],0x04

         mov al,[0x7c00+number+0x00]
         add al,0x30
         mov [es:0x22],al
         mov byte [es:0x23],0x04

         mov byte [es:0x24],'D'
         mov byte [es:0x25],0x07

   infi: jmp near infi                 

  number db 0,0,0,0,0

  times 203 db 0
            db 0x55,0xaa
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
chaox
  • 51
  • 6
  • 1
    Are you sure the code you gave us doesn't work? Is it possible that in your real code you are displaying characters from a string variable (and that fails)? – Michael Petch Mar 10 '16 at 08:26
  • 4
    I have hunch there is more to this issue than the code you are showing.If you edited your question with your complete code for the boot sector we may be able to better troubleshoot. The other possibility is you are not using bochs properly. – Michael Petch Mar 10 '16 at 08:37
  • Did you remember to set the VGA hardware into 80x25, single-plane text mode? Bootloaders do this automatically for you (early kernel code needs (easy) access to VGA before any driver is loaded). However, I can't remember if this is the default behavior. – 3442 Mar 10 '16 at 20:38
  • @KemyLand : this morning when I had a chance, I took this code as is with the exception of adding `[BITS 16]` `org 0x7C00` at the top a `jmp $` after the screen update, and a bootloader signature, put it on the MBR of a 2.88mb disk image, loaded it in bochs and an `L` appeared in the upper left corner of the display (it overwrote part of the BIOS text), but it was visible. Same disk image worked with VirtualBox. I did the experiment with the expectation that the code provided in the question actually looks fine, and should work. – Michael Petch Mar 10 '16 at 21:41
  • @KemyLand : But my simple test worked. I actually then tried both VGA BIOSes that come with Bochs and it worked. The only other possibility I can think of is that on some particular version of Bochs, if you put `jmp $` right after a screen update, the virtual environment goes in an infinite loop and the screen never gets refreshed. We don't know how he ended his bootloader code, but one way around that possible issue is something like `cli` `endloop: hlt` `jmp endloop`. – Michael Petch Mar 10 '16 at 21:48
  • Something tells me that the issue the OP has, is related to code not provided. – Michael Petch Mar 10 '16 at 21:49
  • My first hunch was the OP might not be setting the _DS_ register properly upon entry to the bootloader, and was reading characters from an array of characters (forming a string) attempting to put them on the screen. If you do that with an improper _DS_ then you'll likely get nothing but a blank space (or random characters) displayed. That would be because without a proper _DS_ set (or a segment override reading the characters) you'd be pointing at the wrong memory to retrieve the string you want to print. – Michael Petch Mar 10 '16 at 21:51
  • Can you put in your answer the version information from bochs if you run it with this command: `bochs --help` . For instance on my Ubuntu 14.04 system (fully updated) it shows `Bochs x86 Emulator 2.4.6 Build from CVS snapshot, on February 22, 2011 Compiled at Jun 8 2013, 05:16:04` Curious what yours says. – Michael Petch Mar 11 '16 at 03:39
  • To make you life easier change your line that says `times 203 db 0` to `times 510-($-$$) db 0` .The advantage is that it will fill in the right number of bytes without requiring you to keep adjusting the number. – Michael Petch Mar 11 '16 at 03:41
  • There is an adjustment using the `org` directive that could simplify your code a bit (you wouldn't have to keep adding 0x7c00 for each memory access). But I'll tell you about that after you get us the information. As it stands. I built your code using `nasm -f bin bootload.asm bootload.bin`, `dd if=bootload.bin of=floppy.img` . I created a file called `myos` with your bochs config and then ran bochs with `bochs -f myos` and everything seems to work as expected. – Michael Petch Mar 11 '16 at 03:46
  • You do make a false assumption that the _CS_ register contains an expected value (in your case you expect it to be 0x0000). Unfortunately that isn't always guaranteed. I would change your code that does `mov cx, cs` `mov ds, cx` and replace it with `xor cx, cx` `mov ds, cx`. This guarantees that _DS_ will be a segment of 0x0000.Some systems and virtual environments it's possible to have _CS_ with a segment of 0x07c0 and copying that to _DS_ will point into the wrong memory locations. I cover some of these issues in an SO answer [bootloader tips](http://stackoverflow.com/a/32705076/3857942) – Michael Petch Mar 11 '16 at 03:51
  • @MichaelPetch `Bochs x86 Emulator 2.4.6 Build from CVS snapshot` – chaox Mar 11 '16 at 04:09
  • @MichaelPetch `nasm -f bin boot.asm -o boot.bin` – chaox Mar 11 '16 at 04:12
  • @MichaelPetch yes it is `nasm -f bin boot.asm -o boot.bin`.I've changed my previously typed statements. – chaox Mar 11 '16 at 04:15
  • @MichaelPetch just `bochs`,no more options – chaox Mar 11 '16 at 04:20
  • @MichaelPetch all my files located in `/home/chao/` there are `bochsrc.txt` `boot.asm` `floppy.img` – chaox Mar 11 '16 at 04:25
  • Your code as is (without the modifications I suggested) works here on Ubuntu 14.04 using Bochs CVS Snapshot (version 2.4.6). When I run it I get on the very first line `Label offset:00302D` (it overwrites part of the bochs BIOS bootup text). I would uninstall all the bochs files and reinstall them.I wonder if your system is corrupt or something. – Michael Petch Mar 11 '16 at 04:32
  • @MichaelPetch Thank you very much.Maybe my environment is some different from yours but I could not find it out now.I would use VirtualBox for Linux version instead of Bochs as I feel strange to use gnu tools on windows and I like gnu tools. – chaox Mar 11 '16 at 04:47

0 Answers0