Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 29 Mar 2010 12:22:15 +0200
From:      Gary Jennejohn <gary.jennejohn@freenet.de>
To:        Sergio =?ISO-8859-1?Q?Andr=E9s_G=F3mez?= del Real <sergio.g.delreal@gmail.com>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: Problems understanding this piece of code...
Message-ID:  <20100329122215.235ccc92@ernst.jennejohn.org>
In-Reply-To: <ee48a9121003282106t291e047dpef18f31824541cd3@mail.gmail.com>
References:  <ee48a9121003282106t291e047dpef18f31824541cd3@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, 29 Mar 2010 00:06:49 -0400
Sergio Andr__s G__mez del Real <sergio.g.delreal@gmail.com> wrote:

> Hi.
> I can't seem to understand this piece of i386 boot code:
> What I understand is that BIOS loads MBR at 0x7c00, then jumps to the
> code there, code that relocates itself to and jumps to 0x600, but what
> does movw $main-EXEC+LOAD,%si mean? if it's the source address to copy
> from, why is it $main (0x7c00) - EXEC (0x600) + LOAD (0x7c00) above
> main? then, movw $main,%di is the address to copy to... wasn't 0x600
> the address to copy to? then jump to jmp main-LOAD+EXEC (0x600)?
> 
> I guess I am really missing something here, but I can't get to
> understand what's happening.
> 
> start:		cld				# String ops inc
> 		xorw %ax,%ax			# Zero
> 		movw %ax,%es			# Address
> 		movw %ax,%ds			#  data
> 		movw %ax,%ss			# Set up
> 		movw $LOAD,%sp			#  stack
> #
> # Relocate ourself to a lower address so that we are out of the way when
> # we load in the bootstrap from the partition to boot.
> #
> 		movw $main-EXEC+LOAD,%si	# Source
> 		movw $main,%di			# Destination
> 		movw $0x200-(main-start),%cx	# Byte count
> 		rep				# Relocate
> 		movsb				#  code
> #
> # Jump to the relocated code.
> #
> 		jmp main-LOAD+EXEC		# To relocated code
>

The boot loader was linked to run at EXEC, so main is actually EXEC plus
the offset to main from EXEC.

So main-EXEC+LOAD results in calculating the offset to main at the place
where the BIOS loaded it ==> LOAD+offset_to_main.  This must be used as
the source address for the movsb.

--
Gary Jennejohn



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20100329122215.235ccc92>