boot引导程序

boot引导程序


org 07c00h
boot:
jmp begine
nop
BS_OEMName db 'cty1234'
BPB_BytsPerSec dw 512
BPB_SecPerClus  db 1
BPB_RsvdSecCnt  dw 1
BPB_NumFATs	 db 2
BPB_RootEntCnt	 dw 224
BPB_TotSec16	 dw 2880
    BPB_Media	 db 0F0h
    BPB_FAT16	 dw 9
    BPB_SecPerTrk	 dw 18	 ; 每磁道扇区数
BPB_NumHeads	 dw 2	 ; 磁头数(面数)
BPB_HiddSec	 dd 0	 ; 隐藏扇区数
BPB_TotSec32	 dd 0	 ; 如果 wTotalSectorCount 是 0 由这个值记录扇区数
BS_DrvNum	 db 0	 ; 中断 13 的驱动器号
BS_Reserved1	 db 0	 ; 未使用
BS_BootSig	     db 29h	 ; 扩展引导标记 (29h)
BS_VolID	     dd 0	 ; 卷序列号
BS_VolLab	     db 'evilbinary '; 卷标, 必须 11 个字节
BS_FileSysType	 db 'FAT12   '
   INITOFS dw  1000h
INITSEG dw  9000h
FATOFS  dw  0000h
    FATSEG  dw  9000h
fatnum dw 9 
sectnum  dw  19
sectnum1 db 1
fatnumcount dw 1
    fatentry dw 200h
filename db  'SETUP   BIN'
bootm:   db  "Bootting"
nofound: db  "No setup file."
    ready:   db  "Ready."
begine:
mov ax,cs
mov ds,ax
mov ss,ax
mov es,ax
mov sp,ax
mov	ax, 0600h   
mov	bx, 0700h   
mov	cx, 0    
mov	dx, 0184fh   
int	10h
mov ax,0200h
xor dx,dx
mov bx,0
int 10h
xor ax,ax    ;复位
xor dx,dx
int 13h
mov cx,8
mov si,bootm
call print_string
nof:
mov ax,[sectnum]
;mov ax,21
cmp ax,32
ja fail
mov di,[FATOFS]
    mov es,[FATSEG]
    call read_sect
    call search_file
    inc word [sectnum]
    cmp ax,-1
    je nof
    ;ax=file first sector
fatread:
push ax
mov es,[INITSEG]
mov di,[INITOFS]
    add ax,31
call read_sect 
pop ax 
add word[INITOFS],200h
mov bx,[INITOFS]
push ax
mov al,'.'
call print_char
pop ax
call get_entry
cmp ax,0ff8h
jae fatreadend
    jmp fatread
fatreadend: 
 
   call print_newline
 
mov cx,6
    mov si,ready
    call print_string
    
    call print_newline
    
    jmp 0x9000:0x1000
 
fail:
call print_newline
    mov cx,14
    mov si,nofound
    call print_string       
    jmp $
    ;int 3h
    
    
;ax=sector es:di=fat men addr
get_entry:
    push bx
    push cx
    push dx
    push es
    push di
  
    mov bx,3
    mul bx
    mov bx,2
    div bx
    
a: 
    cmp ax,[fatentry]      ;first time=200h
jbe next
mov es,[FATSEG]
mov di,[FATOFS]
push ax
mov ax,[fatnumcount]
cmp ax,9
ja endread           ;fat 9 项            
call read_sect
pop ax
inc word[fatnumcount]
add word[fatentry],200h 
      ;200h
jmp a
next: 
    push ax
    mov es,[FATSEG]
mov di,[FATOFS]
mov ax,[fatnumcount]     
call read_sect
pop ax
    add ax,200h
    sub ax,[fatentry]
    add di,ax
    
    mov ax,[es:di]
    
    cmp dx,0
    je even1
    mov cl,4
    shr ax,cl
    jmp endread
even1:
    and ax,0fffh
endread:    
    ;int 3h
    pop di
    pop es
    pop dx
    pop cx
    pop bx
    ret
 
;es: di=地址 ds: si=filename ax->起始扇区号 ax=-1 没找到
search_file:
    push bx
    push cx
    push si
    cld
    mov bx,di
goon:
    mov di,bx
    ;and di,0ffe0h
    mov si,filename 
     
    mov cx,11
    repz cmpsb
    cmp cx,0
   
    jz find 
   
    add bx,32 
    
    cmp bx,200h ;FATOFS+512byte one sector
     
    jae nofind
    jmp goon
find:
    ;print
    add di,15
    mov ax,[es:di]  
    ;add ax,31 
    jmp e
nofind: 
    
    mov ax,-1
e:
    
    pop si
    pop cx
    pop bx
    ret
;sectornum/18 ch=Q>>1 cl=R+1 dh=Q&1
;ax=扇区号,sectnum1=扇区数 es:di=mem addr
read_sect:
    push ax
    push bx
    push cx
    push dx
    push di
 
    mov bh,18
    div bh
    mov ch,al
    shr ch,1
    and al,1
    mov dh,al
    inc ah
    mov cl,ah
    mov dl,00h
    mov bx,di
readagine:
    ;mov bx,[init_kernel_ofs]
    ;mov es,[init_kernel_seg]
    mov ah,02h
    mov al,1
    int 13h
    jc readagine
    pop di
    pop dx
    pop cx
    pop bx
    pop ax
    ret
print_newline:
    push ax
     mov al,0dh
    call print_char
    mov al,0ah
    call print_char
    pop ax
    ret
;al char
print_char:
    push	ax    
push	bx    
mov	ah, 0eh   
;mov	al, '.'    
mov bx,000ch  
int	10h    
pop	bx  
pop	ax
    ret
;si addr ,cx count ,dh row ,dl col
print_string:
    push ax
ps:
    mov al,[si]
    call print_char
    inc si
    loop ps
pend:
    pop ax    
ret
 
   times 510-($-$) db 0
    dw 0xaa55 
 
;加载setup.bin文件 并跳转过去执行 。