Page 1 of 1

Boot Loader Setup

Posted: Tue Sep 06, 2011 10:59 am
by brodeur235
I am writing a two stage bootloader and the two modules are linking correctly, and when I jump to the second stage, the first stops executing (tested); so the jump is working, however, stage_two (which I jump to) never executes. I think it might have something to do with segment register setup; I haven't done anything with the segment registers yet, but when I try to initialize them with something like:

Code: Select all

mov ax,0x00
mov cs,ax
mov ds,ax
mov es,ax
mov fs,ax
Then code stops executing at that point; so I assume that's wrong..
Here's the source's that pertain:

stage_one.asm:

Code: Select all

extern _stage_two

[BITS 16]

global _start
_start:
	
	; disable interrupts
	cli
	
	; clear screen
	mov ah,0x07
	mov bh,0x07
	mov al,0x00
	mov ch,0x00
	mov cl,0x00
	mov dh,0x19
	mov dl,0x50
	int 0x10
	
	; reposition cursor
	mov ah,0x02
	mov bh,0x00
	mov dh,0x00
	mov dl,0x00
	int 0x10
	
	jmp _stage_two
	
	; Padding and boot flags
	times 510-($-$$) db 0x00
	db 0x55, 0xAA
stage_two

Code: Select all

[BITS 16]

global _stage_two
_stage_two:
	
        ; Code here will not execute.
	
	; LOOP
	jmp $
Help very much appreciated,

Brodeur235

Re: Boot Loader Setup

Posted: Tue Sep 06, 2011 11:10 am
by AJ
Hi,

I'm a little confused to see the use of 'extern' and 'global' in flat binaries.

Have you checked the register dump? Where is CS:IP when stage 1 finishes? Where would you expect it to be? Is stage2 loaded from disk somewhere (the BIOS only loads the boot sector and you need to read the rest yourself, normally via BIOS calls)?

Also, what do you mean by 'stops executing'? Have you hit a hlt instruction? Are you in an infinite loop (jmp $)? Are you executing code from a random piece of uninitialised memory?

More information, please, including a Bochs register dump and a summary of what you would expect to see.

Cheers,
Adam

Re: Boot Loader Setup

Posted: Tue Sep 06, 2011 11:12 am
by AJ
Also:
brodeur235 wrote:

Code: Select all

mov ax,0x00
mov cs,ax
[snip]
You say this is tested - how?

Cheers,
Adam

Re: Boot Loader Setup

Posted: Tue Sep 06, 2011 1:16 pm
by brodeur235
I just asked a really f*cking dumb question. I haven't done OS design in a while please forgive me. I did not load it into memory... Good call

Brodeur235

Re: Boot Loader Setup

Posted: Tue Sep 06, 2011 2:23 pm
by AJ
np - we all make mistakes :)

Re: Boot Loader Setup

Posted: Tue Sep 06, 2011 9:19 pm
by Chandra
brodeur235 wrote: I did not load it into memory... Good call
That's not the only problem. As AJ pointed out, you need to throw out extern and global symbols out of your assembly file. Moreover, make sure the segment registers are initialized properly before you make any BIOS calls.
Cheers!

Re: Boot Loader Setup

Posted: Wed Sep 07, 2011 1:52 am
by mark3094
You may also need to specify where the second stage is executing from. Something like:

Code: Select all

[ORG 0x0500]
I think this is dependant on the assembler. Then the segments can be set to 0x0:

Code: Select all

	CLI											;  Disable interrupts while changing segment registers
	XOR		AX, AX							;  Set AX to zero
	MOV		DS, AX							;  Set DS and ES to zero. This really sets DS and ES to the location we are executing from
	MOV		ES, AX
	MOV		SS, AX							;  SS:SP is the location of the stack
	MOV		SP, 0xFFFF					  ;  SS:SP = 0000:FFFF
	STI											;  Enable interrupts