红联Linux门户
Linux帮助

自己写的l引导程序加载后的0x7c00处怎么不是程序开始的部分

发布时间:2011-09-24 22:41:52来源:红联作者:quickj
自己写了个引导程序,但调试后发现,0x7c00处的指令不是 jmp short quick_j_start,也不是 mov ax,cs 而是偏移段首
一定的距离的代码。发现段首部分的一些代码丢失了,反汇编了很多内存也找不到那些代码。
求内核的高手们帮帮忙。

下面是源代码:

BaseOfStack equ 07c00h ; Boot状态下堆栈基地址(栈底, 从这个位置向低地址生长)
org 0x7c00
%include "load.inc"


%include "fat12headet.inc"

jmp short quick_j_start
nop


quick_j_start:
mov ax,cs
mov ds,ax
mov es,ax
mov ss,ax
mov sp,BaseOfStack

;清屏
mov ax,0600h
mov bx,0700h
mov cx,0
mov dx,0184h
int 10h
;显示booting...
push 10
push Bootmessage
call Dispstr

xor ah, ah ;
xor dl, dl ; ┣ 软驱复位
int 13h ; ┛

;用来寻找根目录去的loader程序断
;被读的每一个扇区 没读一次的起始地址加32字节,共读16次,32*16=512 为每个扇区的字节
mov ax,[rootsector]
mov [count],ax
quick_j_findloader:
cmp word [count],0 ;gocout是根目录区的扇区数
jz Noloader
dec word [count]
mov ax,BaseOfLoader
mov es,ax
mov bx,OffsetOfLoader
mov ax,[BeingReadesector] ;BeingReadesector 是被读取的扇区号
mov cl,1
call Readsector


mov si,Loadname
mov di, OffsetOfLoader

cld
mov dx,10h

Searchforloadebin:
cmp dx,0
jz Readnextsector
dec dx
mov cx,11
Searchforloadname:
cmp cx,0
jz LoaderFound
dec cx
lodsb
cmp al,[es:di]
jnz Different
inc di
jmp Searchforloadname

Different:
and di,0ffe0h
add di,20h
mov si,Loadname
jmp Searchforloadebin

Readnextsector:
dec word[count]
inc word [BeingReadesector]
jmp quick_j_findloader


Noloader:

push 9
push noloadermessage
call Dispstr
jmp $
;loader找到的时候
LoaderFound:
mov ax,word [rootsector]
and di,0ffe0h
add di,01ah
mov cx,word [es:di]
push cx ;保存首个蔟号
add cx,ax
add cx, RootDirSectors

mov ax, BaseOfLoader
mov es,ax
mov bx, OffsetOfLoader

mov ax,cx
goonloaderfile:
push bx
push ax

mov cl,1
call Readsector
pop ax
mov cl,2
call Getfatentry
cmp ax,0fffh
jz finishloaderzai
mov cx,ax
add cx, RootDirSectors
add cx,DeltaSectorNo
mov ax, BaseOfLoader
mov es,ax
pop bx
add bx,[BPB_BytsPerSec]
jmp goonloaderfile

finishloaderzai:
push 6
push Readymessage
call Dispstr








count dw 0

rootsector dw 14
bmlength equ 9
Dataroosector equ 33
Firstrootsector equ 19
Wrootsectorfor dw Dataroosector
BeingReadesector dw Firstrootsector

;Message some
noloadermessage db "no loader"
bodd db 0
Readymessage db "redy!!"
Bootmessage db "booting..."
Loadname db "LOADER BIN",0









;=========================================
; 函数名 Dispstr 用于显示字符
;========================================


Dispstr:
push bp
mov bp,sp
mov ax,ds
mov es,ax
mov ax,[bp+6]
mov cx,[bp+4]

mov bp,ax
; CX = 串长度
mov ax, 01301h ;
;AH = 13,
;AL = 01h
mov bx, 0007h ; 页号为0(BH = 0) 黑底白字(BL = 07h)
mov dl, 0
int 10h
pop bp
add sp,4
ret

;=====================
;函数读取扇区数据
;======================
Readsector:

push bp
mov bp,sp
sub sp,2
push bx
mov byte [bp-2],cl
mov bl,[BPB_SecPerTrk]
div bl
inc ah
mov cl,ah
mov dh,al
and dh,1
shr al,1
mov ch,al

pop bx
mov dl,0
Goreding:
mov ah,2
mov al,byte [bp-2]
int 13h

jc Goreding

add esp,2
pop bp
ret

Getfatentry:
push es
push bx
push ax
mov ax,BaseOfLoader
sub ax,0100h
mov es,ax
pop ax
mov bx,3
mul bx
mov bx,2
div bx
cmp dx,0
jz gofat
mov byte[bodd],1
gofat:
xor dx,dx
mov bx,[BPB_BytsPerSec]
div bx

push dx
add ax,SectorNoOfFAT1
mov cl,2
call Readsector
xor bx,bx
pop dx
add bx,dx
mov ax,[es:bx]
cmp byte [bodd],1

jnz gofat2
shr ax,4
gofat2:
and ax,0fffh


pop bx
pop es
ret



times 510-($-$$) db 0
dw 0xaa55
文章评论

共有 0 条评论