Page 1 of 1

Bootloader not handing off execution

Posted: Sun Feb 10, 2013 10:20 am
by supershirobon
So, I'm quite sure I'm not doing anything wrong. Even looked at some other people's code in case my registers were wrong or something, but no.
This is just a simple bootloader I whipped up which will load the next sector on the disk (2nd sector) into memory, then jump to there and continue execution of what was just loaded. (Don't have a file system). The data is loaded using INT 0x13, which loads the sector into ES:BX (0x1000:0) in this case. Then my loaded program should be in memory, the bootloader jumps to where it was loaded and should continue execution. But it doesn't work for some reason.

I'm quite sure the offending code is here, because this is where all my diskette functions are. But I'm not sure what it is because I even checked other people's code so I'm sure I'm doing something fine.

Code: Select all

;--------------Reset diskette--------------------------
;will reset drive 0 and if fails tries again          
resetDiskFail:
	mov si, msg_resetDiskFail
	call write	;will fall down to reset disk again
resetDisk:
	mov si, msg_resetDisk
	call write
	mov ah, 00		;reset disk
	mov dl, 00		;Disk 0 is the default drive
	int 0x13		;call bios and reset
	jc resetDiskFail;if carry flag is set error occured so try again
	jmp loadSector  ;jump to load sectors instead if passed
;---------------Load Sectors------------------------------
;Will load the next sector on the disk into 0x1000:0
loadSectorFail:
	mov si, msg_loadSectorFail
	call write
loadSector:
	mov si, msg_loadSector
	call write
	
	mov ah, 00
	int 0x16		;wait for keypress incase data on another disk
	
	mov ax, 0x1000	;read into address 0x1000:0
	mov es, ax		;sector data is loaded into es:bx, so 0x1000:0 
	xor bx, bx
	
	mov ah, 0x02	;read disk sector
	mov al, 01		;read 1 sector
	mov ch, 01		;reading from track 1
	mov cl, 02		;second sector (next one)
	mov dh, 00		;head 0
	mov dl, 00		;drive 0 (default)
	int 0x13		;call bios and load sector
	jc loadSectorFail; carry will be 1 on error, so if error try again
	
	mov si, msg_loadSectorOK
	call write
	jmp lastStep
;-----------End of the Boot loader--------------------
;Jumps to where we loaded the data into
;and passes off control to whatever lies in the clutches
;of the second sector :)
;execution will be taken off from there!!
lastStep:
	mov ax, 0x1000		;set data segment register
	mov ds, ax
	jmp 0x1000:0x0000	;JUMP AND EXECUTE!!!!!
And my data I'm trying to load is just a simple

Code: Select all

org 0x1000 ;Because this is where we loaded to, so this is where we start, correct?
cli
hlt
I know it's not loading because bochs says nothing about being halted.

Any help would be appreciated :)

Re: Bootloader not handing off execution

Posted: Sun Feb 10, 2013 11:22 am
by trinopoty
Are you booting from floppy? if not, drive 0 is not default. Use single step debugging in Bochs to pinpoint the problem.

Re: Bootloader not handing off execution

Posted: Sun Feb 10, 2013 12:21 pm
by DLBuunk
First of all, the "org 0x1000" should be a "org 0x0", since you start executing at the start of your segment (0x1000). See http://wiki.osdev.org/Real_Mode#Memory_Addressing.

Secondly, you change both code and data segments, but you leave the stack for what it is, which may cause inconvenience later on.

Thirdly, you load from track 1, are you sure this is what you want? (tracks start counting at 0)

Re: Bootloader not handing off execution

Posted: Sun Feb 10, 2013 2:34 pm
by supershirobon
Much thanks. I can't believe I put org 0x1000, I must have been really tired. (wrote this at 4:30 am) :wink:

Didn't know that track starts at 0, but thank you for telling me :)

Thanks a lot, It's working now :D