Bootloader that reads sectors problem

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
LloydAX86
Posts: 6
Joined: Sat Feb 05, 2011 6:32 am

Bootloader that reads sectors problem

Post 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.
Attachments
This is the Boch debug window
This is the Boch debug window
boot2.asm
This is the program that should be loaded
(286 Bytes) Downloaded 68 times
boot.asm
(1.43 KiB) Downloaded 51 times
Last edited by LloydAX86 on Sat Feb 05, 2011 7:48 am, edited 2 times in total.
Dario
Member
Member
Posts: 117
Joined: Sun Aug 31, 2008 12:39 pm

Re: Bootloader that reads sectors problem

Post 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
____
Dario
User avatar
b.zaar
Member
Member
Posts: 294
Joined: Wed May 21, 2008 4:33 am
Location: Mars MTC +6:00
Contact:

Re: Bootloader that reads sectors problem

Post 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.
"God! Not Unix" - Richard Stallman

Website: venom Dev
OS project: venom OS
Hexadecimal Editor: hexed
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: Bootloader that reads sectors problem

Post by Chandra »

Ah, a lot of errors.

Better grab a good tutorial and start again.

Best Regards,
Chandra
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
LloydAX86
Posts: 6
Joined: Sat Feb 05, 2011 6:32 am

Re: Bootloader that reads sectors problem

Post 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
Dario
Member
Member
Posts: 117
Joined: Sun Aug 31, 2008 12:39 pm

Re: Bootloader that reads sectors problem

Post 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.
____
Dario
Post Reply