Problems understanding this piece of code...
gary.jennejohn at freenet.de
Mon Mar 29 10:22:18 UTC 2010
On Mon, 29 Mar 2010 00:06:49 -0400
Sergio Andr__s G__mez del Real <sergio.g.delreal at gmail.com> wrote:
> 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.
More information about the freebsd-hackers