8086CPU寄存器 & linux bootsect.s分析

8086CPU寄存器

  • 有14个寄存器,每个寄存器都有16位长度,分别为AX、BX、CX、DX、SI、DI、SP、BP、IP、CS、SS、DS、ES、PSW。
  • AX、BX、CX、DX被定义为通用寄存器,可以用来暂存数据,互不影响。
  • CS、DS、SS、ES被定义为段寄存器,用来提供内存单元的段地址。
  • 8086CPU不支持将数据直接送入段寄存器的操作,但支持将一个寄存器中的内容送入另一个寄存器(通过mov指令)
  • CS存放当前指令的段地址,DS存放内存单元的段地址,SS存放栈顶的段地址,SP存放栈顶偏移地址,IP存放当前指令偏移地址。

物理地址

  • 段地址X4+指令寄存器的值等于实际执行的物理地址
  • IP被定义为指令寄存器

如何修改当前取址处地址?

  • 使用jmp指令:“jmp 段地址:偏移地址”。例如:jmp 2AE3:3,执行后:CS=2AE3H,IP=0003H,CPU将从2AE33H处读取指令。(段地址和偏移地址共同修改)
  • 使用jmp指令:“jmp 某一合法寄存器”,指令的功能为:用寄存器的值修改IP。例如:jmp ax,指令修改前:ax=1000H,CS=2000H,IP=0003H;指令修改后:AX=1000H,CS=2000H,IP=1000H。(仅修改IP偏移地址)
  • 使用jmpi指令:“jmpi 偏移地址,段地址”。例如:jmpi go,ax,执行后,把go赋给ip,把ax赋给cs。

mov al [0]的指令含义?
"[…]"表示一个内存单元,“[…]”中的0表示内存单元的偏移地址.


如何读取磁盘?

使用中断:int 0x13,以下分析说明:
1、0x13是BIOS读取磁盘扇区的中断:ah=0x02-读磁盘,al=扇区数量(SETUPLEN=4),ch=柱面号,cl=开始扇区,dh=磁头号,dl=驱动器号,es:bx=内存地址。

汇编中的CF是什么意思?
进位标志CF(Carry Flag)。进位标志CF主要用来反映运算是否产生进位或借位。如果运算结果的最高位产生了一个进位或借位,那么,其值为1,否则其值为0。涉及到它的指令有两个:即JC在CF=1时跳转,JNC在CF=0时跳转。


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.
!
! It then loads 'setup' directly after itself (0x90200), and the system
! at 0x10000, using BIOS interrupts. 
!
! 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
!
! 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.

.globl begtext, begdata, begbss, endtext, enddata, endbss
.text
begtext:
.data
begdata:
.bss
begbss:
.text

SETUPLEN = 4				! nr of setup-sectors
BOOTSEG  = 0x07c0			! original address of boot-sector
INITSEG  = 0x9000			! we move boot here - out of the way
SETUPSEG = 0x9020			! setup starts here
SYSSEG   = 0x1000			! system loaded at 0x10000 (65536).
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

entry _start
_start:
	mov	ax,#BOOTSEG	! ax = 0x07c0
	mov	ds,ax		! ds = 0x07c0 data segment
	mov	ax,#INITSEG	! ax = 0x90000
	mov	es,ax		! es = 0x90000 extra segment
	mov	cx,#256		! cx = 256 count register
	sub	si,si		! si = 0 source index
	sub	di,di		! di = 0 destination index
	rep			! 重复操作指令前缀,循环执行movw,且cx递减1,直到cx为0为止
	movw			! 将ds存放地址中的16位bit数据复制到es存放地址中去
	jmpi	go,INITSEG	! 间接跳转,将当前cs赋值0x90000,ip赋值为标签go的地址.标签go的地址等于_start的地址(一般为0)加此时go所在位置的偏移地址
go:	mov	ax,cs		! 其实间接跳转到了此位置,ax = 0x90000
	mov	ds,ax		! ds = 0x90000
	mov	es,ax		! es = 0x90000
! put stack at 0x9ff00.
	mov	ss,ax		! ss = 0x90000 stack segment
	mov	sp,#0xFF00	! sp = 0xff00 stack point	! arbitrary value >>512
! 上面的代码的作用主要是将boot的代码复制到内存中指定位置,然后设置栈(为什么要设置栈?)
! load the setup-sectors directly after the bootblock.
! Note that 'es' is already set up.

load_setup:
	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			! 进入读取磁盘的中断接口(0x13为指定读取磁盘中断号)
	jnc	ok_load_setup		! 条件跳转指令,如果CF等于0(运算没有产生进位或借位),则跳转到ok_load_setup标签地址处执行代码.
	mov	dx,#0x0000		! dx = 0
	mov	ax,#0x0000		! reset the diskette
	int	0x13			! 继续读磁盘(不理解为什么再次读磁盘)
	j	load_setup		! 跳转到load_setup标签处的地址执行代码

ok_load_setup:

! Get disk drive parameters, specifically nr of sectors/track

	mov	dl,#0x00		! 驱动器号 = 0
	mov	ax,#0x0800		! AH=8 is get drive parameters
	int	0x13			! 读取磁盘中驱动器的参数
	mov	ch,#0x00		! ch = 0
	seg cs				! 取cs寄存器中的段地址,仅对下一条指令有效
	mov	sectors,cx		! 将cx中的内容复制到cs:sectors物理地址处
	mov	ax,#INITSEG		! ax = 0x90000 
	mov	es,ax			! es = 0x90000

! Print some inane message

	mov	ah,#0x03		! read cursor pos
	xor	bh,bh			! bh = bh xor bh
	int	0x10			! 进入打印屏幕中断接口
	
	mov	cx,#24			! cx = 24 打印logo的长度
	mov	bx,#0x0007		! page 0, attribute 7 (normal)
	mov	bp,#msg1		! 显示的信息logo
	mov	ax,#0x1301		! write string, move cursor
	int	0x10

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

	mov	ax,#SYSSEG		! ax = 0x10000
	mov	es,ax		! segment of 0x010000 es = 0x10000
	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:
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值