Boot Loader Setup

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
brodeur235
Member
Member
Posts: 86
Joined: Sat Jun 06, 2009 11:55 am

Boot Loader Setup

Post 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
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Boot Loader Setup

Post 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
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Boot Loader Setup

Post by AJ »

Also:
brodeur235 wrote:

Code: Select all

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

Cheers,
Adam
brodeur235
Member
Member
Posts: 86
Joined: Sat Jun 06, 2009 11:55 am

Re: Boot Loader Setup

Post 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
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Boot Loader Setup

Post by AJ »

np - we all make mistakes :)
User avatar
Chandra
Member
Member
Posts: 487
Joined: Sat Jul 17, 2010 12:45 am

Re: Boot Loader Setup

Post 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!
Programming is not about using a language to solve a problem, it's about using logic to find a solution !
User avatar
mark3094
Member
Member
Posts: 164
Joined: Mon Feb 14, 2011 10:32 pm
Location: Australia
Contact:

Re: Boot Loader Setup

Post 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
Post Reply