Page 1 of 1

BootLoader 4

Posted: Mon Mar 08, 2010 8:20 pm
by wererabit
Hi,

I have a question. In the tutorial 6, the section where we only want to print out a message for our 2nd stage boot loader. I see this

Code: Select all

;*************************************************;
;	Second Stage Loader Entry Point
;************************************************;

main:
			cli		; clear interrupts
			push	cs	; Insure DS=CS
			pop	ds

			mov	si, Msg
			call	Print

			cli		; clear interrupts to prevent triple faults
			hlt		; hault the system
My question is why do we have to ensure cs = ds? Do we always have to do this for the second stage bootloader?

Thanks

Re: BootLoader 4

Posted: Mon Mar 08, 2010 10:40 pm
by Mike
Hello,

You dont need to insure ds==cs, the software does that to insure they share the same segment so memory references are the same. ie, ds:Msg points to the right linear address.

Re: BootLoader 4

Posted: Tue Mar 09, 2010 5:23 am
by wererabit
Thanks, Mike. My question is what happens if we don't do that?

Re: BootLoader 4

Posted: Tue Mar 09, 2010 7:02 pm
by Andyhhp
They dont have to be the same, but almost all code assumes that they are.

When you jump to the second stage, the cs segment will be updated ( so relative jumps will work as intended ) but ds wont be updated. If ds isnt updated, then all references to data in stage2 will actually be references to data from the bootloader, which is not a good situation.

When you assemble stage2.asm, the compiler will assume (if you dont provide an ORG directive) that the program is starting at 0x0:0x0 in memory, so all references are relative to offset 0. Then, if you load stage2 elsewhere (e.g. to 0x500 linear address), you can change the segment to 0x50 and the offset to 0, and all the relative references will work.

I hope that explains a bit

~Andrew

Re: BootLoader 4

Posted: Thu Mar 11, 2010 8:49 pm
by wererabit
Thanks for the reply.