linux完全注释1篇

今天开始正式进入博客编写,GOOOOO!!!

bootsect.s   文件注释

<<!
! SYS_SIZE is the number of clicks (16 bytes) to be loaded.
! 0x3000 is 0x30000 bytes = 196kB, more than enough for current
! versions of linux
!
SYSSIZE = 0x3000
!
! bootsect.s (C) 1991 Linus Torvalds
!
! bootsect.s is loaded at 0x7c00 by the bios-startup routines, and moves
! iself out of the way to address 0x90000, and jumps there.
<<!


! SYSSIZE 等于0x3000表示system模块总的大小 30000bytes,共192KB

! bios将bootsect.s载入到0x7c00,并把自己移动到0x90000,并且跳到此处 

(准确来说是bois加电自检完成后,按启动顺序寻找启动扇区,找到启动扇区后,将启动扇区读入内存0x7c00:0000处,然后cs:ip指向该地址,并从该地址开始执行)

!!! 此处有些疑问,二进制指令与bootsect.s的对应问题,汇编指令怎样对应到内存中某一地址的,估计涉及到编译器、连接器的知识

>>以下是 于渊十分钟完成操作系统--boot.asm编译后的二进制指令

8C C8 8E D8 8E C0 E8 02 00 EB FE B8 1E 7C 89 C5 B9 10 00 B8 01 13 BB 0C 00 B2 00 CD 10 C3 48 65 6C 6C 6F 2C 20 4F 53 20 77 6F 72 6C 64 21 00 00 00 00 00

>>以下是bios将二进制文件载入内存后,内存中存放的内容

0x00007c00 <bogus+       0>:	0x8c	0xc8	0x8e	0xd8	0x8e	0xc0	0xe8	0x02
0x00007c08 <bogus+       8>:	0x00	0xeb	0xfe	0xb8	0x1e	0x7c	0x89	0xc5
0x00007c10 <bogus+      16>:	0xb9	0x10	0x00	0xb8	0x01	0x13	0xbb	0x0c
0x00007c18 <bogus+      24>:	0x00	0xb2	0x00	0xcd	0x10	0xc3	0x48	0x65
0x00007c20 <bogus+      32>:	0x6c	0x6c	0x6f	0x2c	0x20	0x4f	0x53	0x20
0x00007c28 <bogus+      40>:	0x77	0x6f	0x72	0x6c	0x64	0x21	0x00	0x00
0x00007c30 <bogus+      48>:	0x00	0x00
00007c00: (                    ): mov ax, cs                ; 8cc8
00007c02: (                    ): mov ds, ax                ; 8ed8 
00007c04: (                    ): mov es, ax                ; 8ec0 
00007c06: (                    ): call .+2                  ; e80200 
00007c09: (                    ): jmp .-2                   ; ebfe 
00007c0b: (                    ): mov ax, 0x7c1e            ; b81e7c 
00007c0e: (                    ): mov bp, ax                ; 89c5 
00007c10: (                    ): mov cx, 0x0010            ; b91000 
00007c13: (                    ): mov ax, 0x1301            ; b80113 
00007c16: (                    ): mov bx, 0x000c            ; bb0c00 
00007c19: (                    ): mov dl, 0x00              ; b200 
00007c1b: (                    ): int 0x10                  ; cd10 
00007c1d: (                    ): ret                       ; c3 
00007c1e: (                    ): dec ax                    ; 48 
00007c1f: (                    ): insb byte ptr es:[di], dx ; 656c 
00007c21: (                    ): insb byte ptr es:[di], dx ; 6c 
00007c22: (                    ): outsw dx, word ptr ds:[si] ; 6f 
00007c23: (                    ): sub al, 0x20              ; 2c20 
00007c25: (                    ): dec di                    ; 4f 
00007c26: (                    ): push bx                   ; 53 
00007c27: (                    ): and byte ptr ds:[bx+111], dh ; 20776f 
00007c2a: (                    ): jb .+108                  ; 726c 
00007c2c: (                    ): and word ptr fs:[bx+si], ax ; 642100 
00007c2f: (                    ): add byte ptr ds:[bx+si], al ; 0000 

汇编器和编译器怎么将源码编译、链接到二进制指令文件,还不懂,先去看看编译器和连接器,今天就到这了

GOOOOO!!! 

在这简单说下链接器、加载器的作用,linker 和 loader的主要作用就是解决地址的重定位(地址映射),将抽象的名字(标示符)映射到更低层的名字(该底层名字通常与地址相关联)


!!! 昨天调试了一下代码,org 0x7c00,该汇编语句的主要作用是让后面的代码中使用的offset(偏移量)与内存中信息对应起来


! It then loads 'setup' directly after itself (0x90200), and the system
! at 0x10000, using BIOS interrupts. 

! bootsect.s 使用BIOS中断,直接将setup.s模块,system.s模块载入到他后面0x90200地址处
!
! NOTE! currently system is at most 8*65536 bytes long. This should be no
! problem, even in the future. I want to keep it simple. This 512 kB
! kernel size should be enough, especially as this doesn't contain the
! buffer cache as in minix
! 现在system最多只有8*65536字节(192KB)

!
! The loader has been made as simple as possible, and continuos
! read errors will result in a unbreakable loop. Reboot by hand. It
! loads pretty fast by getting whole sectors at a time whenever possible.
! loader实现的尽可能简单,当有读写错误时,会进入不可中断的循环,这时只有手动重启。
! 这样无论何时,一次载入一个扇区,速度会快很多

!bootsect.s 和 setup.s 都是使用as86来汇编的,其资料相对较少,采用的是intel汇编格式

.globl begtext, begdata, begbss, endtext, enddata, endbss !定义全局变量,告诉汇编器,这些标识符,链接器要用到,需在目标文件的符号表将其标记为全局标识符
.text !定义一个代码段
begtext:
.data !定义一个数据段
begdata:
.bss! 通常用来存放程序中未初始化的全局变量的一个内存区域,wiki bss,http://en.wikipedia.org/wiki/Block_Started_by_Symbol
begbss:
.text


SETUPLEN = 4 ! nr of setup-sectors 定义扇区的个数
BOOTSEG  = 0x07c0 ! original address of boot-sector boot扇区的开始地址
INITSEG  = 0x9000 ! we move boot here - out of the way 将boot移动到此地址处
SETUPSEG = 0x9020 ! setup starts here setup开始模块
SYSSEG   = 0x1000 ! system loaded at 0x10000 (65536).system模块载入地址
ENDSEG   = SYSSEG + SYSSIZE ! where to stop loading 装载结束地址


! ROOT_DEV: 0x000 - same type of floppy as boot.
! 0x301 - first partition on first drive etc
ROOT_DEV = 0x306
 !设备号 = 主设备号 * 256 + 次设备号   (dev_no = (major << 8) + minor)。这里的主设备号是事先定义好的(1-内存,2-磁盘,3-硬盘,4-ttyx,5-tty,6-并行 ! 口,7-非命名管道),譬如对于硬盘,主设备号为3,所以,0x306表示 第二块硬盘的第一个分区
!在此之前  我们先看看 bios执行后,内存布局




entry start   ! 标识start为程序的入口地址,与main类似
start:
mov  ax,#BOOTSEG
mov  ds,ax
mov  ax,#INITSEG
mov  es,ax
mov  cx,#256
sub  si,si
sub di,di
rep
movw
jmpi go,INITSEG !执行后会改变cs:ip的值,cs:ip = 0x9000:offset(go)  此时已将0x07c0:0000-0x07e0:0000的代码拷贝到0x9000:0000-0x9000:0200处
mov ds,ax
mov es,ax
! put stack at 0x9ff00.
mov ss,ax
mov sp,#0xFF00! arbitrary value >>512   ,设置堆栈地址


! load the setup-sectors directly after the bootblock.
! Note that 'es' is already set up.


load_setup:    !开始载入setup.s 程序
mov dx,#0x0000! drive 0, head 0
mov cx,#0x0002! sector 2, track 0
mov bx,#0x0200! address = 512, in INITSEG
mov ax,#0x0200+SETUPLEN! service 2, nr of sectors
int 0x13! read it
jnc ok_load_setup! ok - continue
mov dx,#0x0000
mov ax,#0x0000! reset the diskette
int 0x13
j load_setup


ok_load_setup:


! Get disk drive parameters, specifically nr of sectors/track
!获取驱动器参数

mov dl,#0x00
mov ax,#0x0800! AH=8 is get drive parameters
int 0x13
mov ch,#0x00
seg cs
mov sectors,cx
mov ax,#INITSEG
mov es,ax


! Print some inane message
!打印提示信息

mov ah,#0x03! read cursor pos
xor bh,bh
int 0x10

mov cx,#24
mov bx,#0x0007! page 0, attribute 7 (normal)
mov bp,#msg1
mov ax,#0x1301! write string, move cursor
int 0x10


! ok, we've written the message, now
! we want to load the system (at 0x10000)

!将system载入到0x10000处
mov ax,#SYSSEG
mov es,ax! segment of 0x010000
call read_it
call kill_motor


! After that we check which root-device to use. If the device is
! defined (!= 0), nothing is done and the given device is used.
! Otherwise, either /dev/PS0 (2,28) or /dev/at0 (2,8), depending
! on the number of sectors that the BIOS reports currently.


seg cs
mov ax,root_dev
cmp ax,#0
jne root_defined
seg cs
mov bx,sectors
mov ax,#0x0208! /dev/ps0 - 1.2Mb
cmp bx,#15
je root_defined
mov ax,#0x021c! /dev/PS0 - 1.44Mb
cmp bx,#18
je root_defined
undef_root:
jmp undef_root
root_defined:
seg cs
mov root_dev,ax


! after that (everyting loaded), we jump to
! the setup-routine loaded directly after
! the bootblock:


jmpi 0,SETUPSEG


! This routine loads the system at address 0x10000, making sure
! no 64kB boundaries are crossed. We try to load it as fast as
! possible, loading whole tracks whenever we can.
!
! in: es - starting address segment (normally 0x1000)
!
sread: .word 1+SETUPLEN! sectors read of current track
head: .word 0 ! current head
track: .word 0! current track


read_it:
mov ax,es
test ax,#0x0fff
die: jne die ! es must be at 64kB boundary
xor bx,bx ! bx is starting address within segment
rp_read:
mov ax,es
cmp ax,#ENDSEG! have we loaded all yet?
jb ok1_read
ret
ok1_read:
seg cs
mov ax,sectors
sub ax,sread
mov cx,ax
shl cx,#9
add cx,bx
jnc ok2_read
je ok2_read
xor ax,ax
sub ax,bx
shr ax,#9
ok2_read:
call read_track
mov cx,ax
add ax,sread
seg cs
cmp ax,sectors
jne ok3_read
mov ax,#1
sub ax,head
jne ok4_read
inc track
ok4_read:
mov head,ax
xor ax,ax
ok3_read:
mov sread,ax
shl cx,#9
add bx,cx
jnc rp_read
mov ax,es
add ax,#0x1000
mov es,ax
xor bx,bx
jmp rp_read


read_track:
push ax
push bx
push cx
push dx
mov dx,track
mov cx,sread
inc cx
mov ch,dl
mov dx,head
mov dh,dl
mov dl,#0
and dx,#0x0100
mov ah,#2
int 0x13
jc bad_rt
pop dx
pop cx
pop bx
pop ax
ret
bad_rt: mov ax,#0
mov dx,#0
int 0x13
pop dx
pop cx
pop bx
pop ax
jmp read_track


/*
 * This procedure turns off the floppy drive motor, so
 * that we enter the kernel in a known state, and
 * don't have to worry about it later.
 */
kill_motor:
push dx
mov dx,#0x3f2
mov al,#0
outb
pop dx
ret


sectors:
.word 0


msg1:
.byte 13,10
.ascii "Loading system ..."
.byte 13,10,13,10


.org 508
root_dev:
.word ROOT_DEV
boot_flag:
.word 0xAA55


.text
endtext:
.data
enddata:
.bss
endbss:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值