Page 1 of 1

Bootloader that reads sectors problem

Posted: Sat Feb 05, 2011 7:20 am
by LloydAX86
Hi,

I've been reading several assembler tutorials about printing text to the screen (The one here on OSDev was the main one I used :D ) and also how to load sectors from a floppy into memory. After getting the text working, I went onto resetting the FDC and then *attempting* to load some sectors and then jump to them.

However, it seems to be having problems. As far as I can tell, it resets the FDC and reads the sectors OK, but seems to never execute what has been loaded into memory (I assume this is relevant: Bochs takes about 30 seconds from the message "Loading sectors into memory..." and "Done!", and then displays "Jumping to memory..." and at this point the loaded program should display "Hello, World!" but it never gets this far. QEMU displays a load of random ASCII characters where the word "Jumping" should be).

I've tried many different things to get it working, but with no luck! I've attached the code in the hope that someone can tell me what I'm doing wrong (I wouldn't be surprised if it was something obvious to anyone else who has written a bootloader, but this is my first attempt).

Thank you very much :D
Lloyd

PS: I use

Code: Select all

cat boot.bin boot2.bin > fullboot.bin
to concatenate the files, and then I use

Code: Select all

dd if=/home/lloyd/fullboot.bin of=/dev/fd0
to write it to the floppy.

Re: Bootloader that reads sectors problem

Posted: Sat Feb 05, 2011 7:36 am
by Dario
You don't have 16 bits directive. Haven't checked the rest of the code.

EDIT: Also you should use far jump to new segment es:bx

Re: Bootloader that reads sectors problem

Posted: Sat Feb 05, 2011 7:00 pm
by b.zaar

Code: Select all

read:
	mov ax, 0x1000	;Load sectors to this address

	mov es, ax	;Not really sure what these are for
	xor bx, bx	

	mov ah, 0x02	;Function 2
	mov al, 5	;Number of sectors to load
	mov ch, 1	;Cyclinder number
	mov cl, 1	;Sector 2
	mov dh, 0	;Head 0
	mov dl, 0	;Drive 0
	int 0x13	;Interrupt

	jc read		;Error, try again

	mov si, done	;Tell user it has finished
	call print
	
	ret

Code: Select all

	mov si, memjump	;Tell user about to jump to program
	call print

	jmp 0x1000	;Jump to address
This would be the problem. The part "Not really sure what these are for" sets the ES:BX to load the sectors to 0x1000:0000 but you then jump to 0x07c0:1000.
Also for the read interrupt CH = 0 and CL = 2 for sector 2.

As Dario said you need a bits 16 (nasm) directive and to use a far jump to 0x1000:0000.

If you wanted boot2 at 0x0000:1000 you'll need to set ES:BX differently then far jump to 0x0000:0x1000 and include an org directive to tell the code where it starts in memory

In boot2.asm use this:

Code: Select all


	org     0x1000
	bits    16

	xor     ax,ax
	mov     ds,ax
	mov     es,ax

;Small program to be loaded by bootloader
	mov si, begin
	call print

	mov si, halt
	call print
	hlt

Lastly you need to pad boot.asm to 512 bytes and include the word 0xaa55 at 0x01FE. That way boot2 will get written to sector 2.

Re: Bootloader that reads sectors problem

Posted: Sat Feb 05, 2011 11:23 pm
by Chandra
Ah, a lot of errors.

Better grab a good tutorial and start again.

Best Regards,
Chandra

Re: Bootloader that reads sectors problem

Posted: Sun Feb 06, 2011 7:40 am
by LloydAX86
Hi,

Thanks for all the help and replies :D I've added the boot signature and padded the file, and the org and 16 bit directives to the code. I've also changed the sector/cyclinder it will try to read from.

I've been reading about far jumps on the internet, and as far as I can tell, all I need to do is:

Code: Select all

jmp 0x1000:0000
Although this seems a bit too simple?

I assembled boot.asm and boot2.asm and then wrote it to the floppy, but it gets as far as saying "Jumping to memory..." and then stops.

Thanks again for the help
Lloyd

Re: Bootloader that reads sectors problem

Posted: Sun Feb 06, 2011 9:09 am
by Dario
Show us your new code...and yes, there are many errors, especially your "ret" instruction which doesn't make any sense. That only shows that you haven't done any serious research before writing the code and that you don't understand the platform.

near jump, far jump, call, far call, return, far return...all behave very different under real mode and protected mode and some of them affect the stack. So, you have wonderful manuals from Intel, 2A and 2B are instruction references(read about each instruction before you use it), 1 will explain basic execution environment and programming and 3A goes very detailed about it.
Key is to understand what you're doing and not just to achieve "Hello, World!" boot loader.
It's always hard to lay down the foundations, but once they are strong enough you'll be far more productive.
I've been reading about far jumps on the internet, and as far as I can tell, all I need to do is:

Code: Select all

jmp 0x1000:0000
No, again....try to find out how does the processor translate logical to linear to physical address in real mode.