Bochs vs. Virtual PC 2007- bootloader issues

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Cyclometh
Posts: 2
Joined: Mon Jun 15, 2009 4:28 pm

Bochs vs. Virtual PC 2007- bootloader issues

Post by Cyclometh »

I sincerely hope this isn't too rudimentary a question. I've searched the Wiki and the forums (and the web), but I've had no luck.

I'm tinkering with bootloaders (passionate autodidact, I like to learn how things work by either building one or tearing one apart) and had some trouble working with one on Virtual PC 2007. I've got it down to a very simple repro. My fear is that I'm missing something obvious, but damned if I know what.

To be as totally simple as possible, I'm using a "hello world" bootloader and not my own.

This code (from here: http://www.viralpatel.net/taj/tutorial/ ... loader.php)

Code: Select all

[BITS 16]	;Tells 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

MOV SI, HelloString ;Store string pointer to SI
CALL PrintString	;Call print string procedure
JMP $ 		;Infinite loop, hang it here.


PrintCharacter:	;Procedure to print character on screen
	;Assume that ASCII value is in register AL
MOV AH, 0x0E	;Tell BIOS that we need to print one charater on screen.
MOV BH, 0x00	;Page no.
MOV BL, 0x07	;Text attribute 0x07 is lightgrey font on black background

INT 0x10	;Call video interrupt
RET		;Return to calling procedure



PrintString:	;Procedure to print string on screen
	;Assume that string starting pointer is in register SI

next_character:	;Lable to fetch next character from string
MOV AL, [SI]	;Get a byte from string and store in AL register
INC SI		;Increment SI pointer
OR AL, AL	;Check if value in AL is zero (end of string)
JZ exit_function ;If end then return
CALL PrintCharacter ;Else print the character which is in AL register
JMP next_character	;Fetch next character from string
exit_function:	;End label
RET		;Return from procedure


;Data
HelloString db 'Hello World', 0	;HelloWorld string ending with 0

TIMES 510 - ($ - $$) db 0	;Fill the rest of sector with 0
DW 0xAA55			;Add boot signature at the end of bootloader
When built with NASM and written to a floppy image with FAT_IMGEN, will output "Hello World" just dandy on a Bochs VM. But on Virtual PC 2007, it simply fills the screen with blank characters and ends with a little smiley face character. Very odd. I've tried a few variants of "hello world" boot loaders from various sources on the assumption that my problem was somewhere in my code and tested, simple prototypes would be better.

However, they all exhibit pretty much the same behavior.

Is there some aspect of Virtual PC's emulation I'm not aware of? I'm less concerned about getting VPC to work as I am about understing WHY this is happening.

Thanks!

Cyclo
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Bochs vs. Virtual PC 2007- bootloader issues

Post by ru2aqare »

Code: Select all

[BITS 16]	;Tells 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

MOV SI, HelloString ;Store string pointer to SI
CALL PrintString	;Call print string procedure
JMP $ 		;Infinite loop, hang it here.


The problem is that the code assumes that the segment registers will contain zero and upon entry, IP will contain 7C00h. Which is apparently not the case on VPC. You always have to reload your segment registers in a boot sector - don't assume the BIOS will put there what you want.

Code: Select all

[bits 16 and other nasm-specific stuff]
org 0 ; make sure segment registers will contain 7C0 (set below) and assume the code will run at 7C0:0 (=07C00h)
db 0EAh ; opcode for jmp far 07C0h:here
dw offset here
dw 07C0h
here:
push cs ; make sure ds: is set to whatever cs: is, so that PrintString load the string from where you want it to
pop ds
mov si, offset HelloString
call PrintString
jmp $
Cyclometh
Posts: 2
Joined: Mon Jun 15, 2009 4:28 pm

Re: Bochs vs. Virtual PC 2007- bootloader issues

Post by Cyclometh »

I see- thanks for the response. That puts me back on the right track, I appreciate it!
Post Reply