日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

啟動(dòng)扇區(qū)代碼分析 FreeBSD 5.2.1 boot0( 二 )



下面,我們對(duì)FreeBSD啟動(dòng)扇區(qū)代碼boot0.s進(jìn)行逐步分析 。boot0.s代碼如下:

#
# Copyright (c) 1998 Robert NordIEr
# All rights reserved.
#
# Redistribution and use in source and binary forms are freely
# permitted provided that the above copyright notice and this
# paragraph and the following disclaimer are duplicated in all
# such forms.
#
# This software is provided "AS IS" and without any express or
# implied warranties, including, without limitation, the implied
# warranties of merchantability and fitness for a particular
# purpose.
#
以上的Coyright就不用翻譯了 。

# $FreeBSD: src/sys/boot/i386/boot0/boot0.s,v 1.27 2003/11/20 20:28:18 jhb Exp $
以上供版本管理軟件使用


# A 512-byte boot manager.

.set NHRDRV,0x475 # Number of hard drives
.set ORIGIN,0x600 # Execution address
.set FAKE,0x800 # Partition entry
.set LOAD,0x7c00 # Load address

.set PRT_OFF,0x1be # Partition table

.set TBL0SZ,0x3 # Table 0 size
.set TBL1SZ,0xb # Table 1 size

.set MAGIC,0xaa55 # Magic: bootable
.set B0MAGIC,0xbb66 # Identification

.set KEY_ENTER,0x1c # Enter key scan code
.set KEY_F1,0x3b # F1 key scan code
.set KEY_1,0x02 # #1 key scan code
#
# Addresses in the sector of embedded data values.
# Accessed with negative offsets from the end of the relocated sector (雙).
#
.set _NXTDRV,-0x48 # Next drive
.set _OPT,-0x47 # Default option
.set _SETDRV,-0x46 # Drive to force
.set _FLAGS,-0x45 # Flags
.set _TICKS,-0x44 # Timeout ticks
.set _FAKE,0x0 # Fake partition entry
.set _MNUOPT,0xc # Menu options


以上是定義相關(guān)的參數(shù)值,例如“.set NHRDRV,0x475”類似于C語言中的“#define NHRDRV 0x475”

.globl start # Entry point
.code16 # This runs in real mode

#
# Initialise segments and registers to known values.
# segments start at 0.
# The stack is immediately below the address we were loaded to.
#
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


以上代碼:
1)首先使用“cld”指令清除方向標(biāo)志,使得以下的進(jìn)行“rep”操作時(shí)SI和DI的值遞增 。
2)使ax清零,并使除代碼段cs外的另外兩個(gè)數(shù)據(jù)段寄存器es、ds和堆棧段ss清零 。當(dāng)然,此時(shí)cs
由于reset或初始上電已經(jīng)為零了 。
3)BIOS已經(jīng)把引導(dǎo)扇區(qū)的512字節(jié)的內(nèi)容讀入到了0:0x7c00處,movw $LOAD,%sp 使得堆棧指針指向扇區(qū)
代碼(或曰本段代碼 0:0x7c00)的頂部 。雖然堆棧向下生長可能會(huì)影響代碼的內(nèi)容,但下面我
們馬上就把位于0:7c00處代碼移到其他地方去執(zhí)行 。


#
# Copy this code to the address it was linked for
#
movw %sp,%si # Source
movw $start,%di # Destination
movw $0x100,%cx # Word count
rep # Relocate
movsw # code

把位于0:7c00處的代碼搬移到0:0x600處 。注意,此時(shí)由于代碼連接的重定向,$start=0x600 。

#
# Set address for variable space beyond code, and clear it.
# Notice that this is also used to point to the values embedded in the block,
# by using negative offsets.
movw %di,%bp # Address variables
movb $0x8,%cl # Words to clear
rep # Zero
stosw # them

通過以上一段代碼的執(zhí)行,本代碼已被搬移到0:0x600處,此時(shí)si=di=0x600 0x100,以上代碼
把di的值保存到bp,bp此時(shí)指向本程序搬移后的未用的空間的首部,且把此bp所指的16字節(jié)空間
清零 。以上過程如下圖所示:

┏>0:0x600 ┏━━━━━┓
┃ ┃ ┃
┃ ┃ 搬 ┃
┃ ┃ 移 ┃
┃ ┃ 之 ┃
┃ ┃ 后 ┃
┃ ┃ 的 ┃
┃ ┃ 代 ┃
┃ ┃ 碼 ┃
┃ ┃ ┃
┃ 0:0x7ff ┣━━━━━┫
┃ ┃ 0 ┃

推薦閱讀