OS 超脑02

练习2

切换至32x保护模式

A 设计内存分配

OS 超脑02_第1张图片
a.png

B 实现

SECTION MBR vstart=0x7c00 align=16
    mov ax,0
    mov ss,ax
    mov sp,0
    
    ;初始化内存0x20000
    ;------------------------- EQU -------------------------
    EQU_DISK_IO_0:          equ 0x1f2
    EQU_DISK_IO_1:          equ 0x1f0
    EQU_DISK_COMMOND_READ:  equ 0X20
    EQU_DISK_LBA:           equ 0xe0
    EQU_DISK_RM_GUIDE_ALLOC:equ 0x2000
    ;------------------------- MBR -------------------------
    MBR_START:
        
    MBR_READ_RM_GUIDE_LENGTH:
    
        mov  ax,TEST_END
        
    MBR_LOAD_RM_GUIDE:
        
        push ax
        push 0
        push EQU_DISK_RM_GUIDE_ALLOC
        push 0
        push 0
        push 0
        push 1
        push 1
        
        call MBR_FUNCTION_DISK_LOAD
    MBR_PROTECT_CHANGE:
         mov ax,[cs:gdt_base+0x7c00]
         mov dx,[cs:gdt_base+0x7c00+0x02]
         mov bx,16        
         div bx            
         mov ds,ax
         mov bx,dx
      
         mov dword [bx+0x00],0x00
         mov dword [bx+0x04],0x00  

         mov dword [bx+0x08],0x000001ff     
         mov dword [bx+0x0c],0x00409802     
 
         mov dword [bx+0x10],0x8000ffff     
         mov dword [bx+0x14],0x0040920b     
         
         mov word [cs: gdt_size+0x7c00],23 
                                             
         lgdt [cs: gdt_size+0x7c00]
      
         in al,0x92
         or al,0000_0010B
         out 0x92,al

         cli
         
         mov eax,cr0
         or eax,1
         mov cr0,eax
      
         jmp dword 0x0008:0
    MBR_LOAD_REMLOC:
        
        jmp far [0]
        
    MBR_FUNCTION_DISK_LOAD:
        ;读取的扇区数
        ;起始逻辑扇区
        ;起始逻辑扇区
        ;起始逻辑扇区
        ;起始逻辑扇区
        ;目标内存地址
        ;目标内存偏移
        ;读取字节数量

        pop si

        pop ax
        
        mov dx,EQU_DISK_IO_0
        out dx,al
        
        pop ax
        
        inc dx
        out dx,al

        pop ax
        
        inc dx
        out dx,al

        pop ax

        inc dx
        out dx,al
        
        pop ax
        
        inc dx
        add al,EQU_DISK_LBA
        out dx,al
        
        inc dx
        mov al,EQU_DISK_COMMOND_READ
        out dx,al
        
        pop ds
        pop bx
        
        .MBR_WAIT:
            in  al,dx
            and al,0x88
            cmp al,0x08
            jnz .MBR_WAIT

        pop cx
        mov dx,EQU_DISK_IO_1
        
        .MBR_READ:
            in   ax,dx
            mov  [bx],ax
            add  bx,2
            loop .MBR_READ
        
        push si
        ret
    MBR_FINISH:
        hlt
        gdt_size         dw 0
        gdt_base         dd 0x00010000
        resb 446-($-$$)
        
    MBR_DPT:
        resb 64-($-MBR_DPT)

    MBR_FINISH_MAGIC_CODE:
        db 0x55,0xaa
        
SECTION PROTECT align=16
    TEST_START:
        [bits 32] 
        mov cx,00000000000_10_000B
        mov ds,cx

        ;以下在屏幕上显示"Protect mode 32." 
        mov byte [0x00],'P'  
        mov byte [0x02],'r'
        mov byte [0x04],'o'
        mov byte [0x06],'t'
        mov byte [0x08],'e'
        mov byte [0x0a],'c'
        mov byte [0x0c],'t'
        mov byte [0x0e],' '
        mov byte [0x10],'m'
        mov byte [0x12],'o'
        mov byte [0x14],'d'
        mov byte [0x16],'e'
        mov byte [0x18],' '
        mov byte [0x1a],'3'
        mov byte [0x1c],'2'

        hlt
    TEST_END:

PS:总是忘了把硬盘上的数据移到内存段。一定是MBR写多了

你可能感兴趣的:(OS 超脑02)