From owner-freebsd-hackers@FreeBSD.ORG Mon Mar 29 10:22:18 2010 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 45600106564A for ; Mon, 29 Mar 2010 10:22:18 +0000 (UTC) (envelope-from gary.jennejohn@freenet.de) Received: from mout2.freenet.de (mout2.freenet.de [IPv6:2001:748:100:40::2:4]) by mx1.freebsd.org (Postfix) with ESMTP id D0CB58FC0C for ; Mon, 29 Mar 2010 10:22:17 +0000 (UTC) Received: from [195.4.92.26] (helo=16.mx.freenet.de) by mout2.freenet.de with esmtpa (ID gary.jennejohn@freenet.de) (port 25) (Exim 4.72 #2) id 1NwC6q-0006U3-Gg; Mon, 29 Mar 2010 12:22:16 +0200 Received: from p57ae0df7.dip0.t-ipconnect.de ([87.174.13.247]:33888 helo=ernst.jennejohn.org) by 16.mx.freenet.de with esmtpa (ID gary.jennejohn@freenet.de) (port 25) (Exim 4.72 #2) id 1NwC6q-0003aO-8w; Mon, 29 Mar 2010 12:22:16 +0200 Date: Mon, 29 Mar 2010 12:22:15 +0200 From: Gary Jennejohn To: Sergio =?ISO-8859-1?Q?Andr=E9s_G=F3mez?= del Real Message-ID: <20100329122215.235ccc92@ernst.jennejohn.org> In-Reply-To: References: X-Mailer: Claws Mail 3.7.5 (GTK+ 2.18.7; amd64-portbld-freebsd9.0) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: freebsd-hackers@freebsd.org Subject: Re: Problems understanding this piece of code... X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: gary.jennejohn@freenet.de List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 29 Mar 2010 10:22:18 -0000 On Mon, 29 Mar 2010 00:06:49 -0400 Sergio Andr__s G__mez del Real 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