⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 miniker.asm

📁 自己动手写操作系统源代码,不可多得的代码
💻 ASM
📖 第 1 页 / 共 5 页
字号:
;;------------------------------------------------------------------------
;;    Original Author                   : Garry
;;    Original Date                     : Mar,27,2004
;;    Original Finished date            :
;;    Module Name                       : miniker.asm
;;    Usage                             : countains the mini-kernal code
;;    Defined procedure                 :
;;                                        1.
;;                                        2.
;;                                        3.
;;    Last modified author              : Garry
;;    Last modified date                :
;;    Last modified content             :
;;------------------------------------------------------------------------

;;----------------------- ** Predefines and macros ** --------------------
;;  The following section defines some definations and macros,which can
;;  be used by the ASM code.
;;  NOTICE: In the following code section,may define other macros to use
;;  by local procedure,but these macros only used by the procedure define
;;  it,not 'GLOBAL'.


    %define DEF_PARA_01    dword [ebp + 0x08]         ;;In order to make the
                                                      ;;local procedures can
                                                      ;;access the parameters
                                                      ;;passed,it can use ebp
                                                      ;;register.
                                                      ;;This definations can
                                                      ;;make the code easy to
                                                      ;;organize.
    %define DEF_PARA_02    dword [ebp + 0x0c]

    %define DEF_FALSE      0x00000000    ;;These two definations indicate
                                         ;;the procedure's executation result.
                                         ;;It simulates the bool data-type.
    %define DEF_TRUE       0xffffffff

    %define DEF_INIT_ESP   0x013ffff0    ;;The initializing value of the ESP
                                         ;;register.
    ;%define DEF_INIT_ESP  0x00001000     ;;---------- ** debug ** --------


    con_org_start_addr equ 0x00002000  ;;When this module,mini-kernal is l-
                                       ;;oaded into memory,it resides at
                                       ;;con_org_start_addr,but we must re-
                                       ;;serve the uper 1M memory,so when
                                       ;;the control of CPU transparent to
                                       ;;the mini-kernal,it first moves i-
                                       ;;tself to con_start_addr(see bellow)
                                       ;;from con_org_start_addr,and then
                                       ;;jump to the proper address to exe-
                                       ;;cute.

    con_start_addr equ 0x00100000  ;;Start linear address of the mini-kernal.

    ;con_start_addr equ 0x00002000  ;;-------------- ** debug ** ----------

    con_mast_start equ 0x00110000  ;;The kermal,master's start address.
    ;con_mast_start equ 0x00010000  ;;------------- ** debug **------------

    con_mini_size  equ 0x00010000  ;;We assume the mini-kernal's size is 64K.
    con_mast_size  equ 0x0008c000  ;;The master's length,not acceed 560k.

;;--------------------------- ** Module header ** ------------------------
;;  The following section is the module's header.
;;------------------------------------------------------------------------


    bits 32                      ;;The mini-kernal is a pure 32 bits OS ker-
                                 ;;nal.
    ;org 0x02000                  ;;-------------- ** debug ** ------------

    org 0x00100000               ;;The mini-kernal is loaded at the start
                                 ;;address 0x100000 of the linear address sp-
                                 ;;ace,so the uper 1M memory is reserved.
                                 ;;The uper 4K of this reserved area is co-
                                 ;;untains the system hardware information
                                 ;;filled by BIOS,and the DISPLAY memory a-
                                 ;;lso resides this area.
                                 ;;In additional,we can make other use of
                                 ;;this reserved memory.


    mov ax,0x010                 
    mov ds,ax
    mov es,ax
    jmp gl_sysredirect           ;;The first part of the mini-kernal image is
                                 ;;data section,so the first instruction must
                                 ;;to jump to the actualy code section,which
                                 ;;start at gl_sysredirect.

;;--------------------------- ** Data section ** -------------------------
;;  The following section defines the system's kernal data structures,such
;;  as the GDT,IDT,and other system variables.
;;------------------------------------------------------------------------

align 8
gl_sysdata_section:              ;;System data section,where countains the s-
                                 ;;ystem tables,such as GDT,IDT,and some ope-
                                 ;;rating system variables.

gl_sysgdt:                       ;;The start address of GDT.
                                 ;;In order to load the mini-kernal,the sys-
                                 ;;tem loader program,such as sysldrd.com(f-
                                 ;;or DOS) or sysldrb(for DISK),have initia-
                                 ;;lized the GDT,and make the code segment
                                 ;;and data segment can address the whole 32
                                 ;;bits linear address.
                                 ;;After the mini-kernal loaded,the control
                                 ;;transform to the OS kernal,so the kernal
                                 ;;will initialize the GDT again,this initi-
                                 ;;alization will make the GDT much proper.
    gl_gdt_null             dd 0 ;;The first entry of GDT must be NULL.
                            dd 0

                                 ;;In this operating system,Hello China,we
                                 ;;arrange the system memory as following:
                                 ;;
                                 ;;  start addr    end addr  size   usage
                                 ;;  ----------------------- ----- --------
                                 ;;  0x00000000 - 0x000fffff  1M   reserved
                                 ;;  0x00100000 - 0x013fffff  20M  os code
                                 ;;  0x00100000 - 0x013fffff  20M  os data
                                 ;;  0x00100000 - 0x013fffff  20M  os stack
                                 ;;  0x01400000 - 0xffffffff       program
                                 ;;
                                 ;;Please note that the OS code area and the
                                 ;;OS data area are overlapped.

    gl_gdt_syscode               ;;The system code segment's GDT entry.
                            ;dw 0x1400    ;;The segment's limit is 20M
                            dw 0xFFFF
                                         ;;Please note that the OS code and
                                         ;;OS data segments include the re-
                                         ;;served uper 1M memory.
                            dw 0x0000
                            db 0x00
                            ;dw 0xc09b    ;;Readable,executeable,and acces-
                            dw 0xCF9B
                                         ;;sable,unit is 4k,instruction a-
                                         ;;ddress attribute is 32 bits.
                            db 0x00

    gl_gdt_sysdata               ;;The system data segment's GDT entry.
                            ;dw 0x1400    ;;The segment's limit is 20M
                            dw 0xFFFF
                            dw 0x0000
                            db 0x00
                            ;dw 0xc093    ;;Readable,writeable,and access-
                            dw 0xCF93
                                         ;;able,unit is 4k.
                            db 0x00

    gl_gdt_sysstack              ;;The system stack segment's GDT entry.
                            ;dw 0x1400    ;;Segment's limit is 20M
                            dw 0xFFFF
                            dw 0x0000    ;;The stack's base address is
                                         ;;0x01000000
                            db 0x00
                            ;dw 0xc093    ;;Readable,writeable,and access-
                            dw 0xCF93
                                         ;;able,unit is 4K,and the default
                                         ;;operand's size is 32 bits.
                            db 0x00


    gl_gdt_sysext                ;;The system extent segment's GDT entry,
                                 ;;this segment will be loaded into es re-
                                 ;;gister.
                            ;dw 0x1400    ;;The segment's limit is 20M
                            dw 0xFFFF
                            dw 0x0000
                            db 0x00
                            ;dw 0xc093    ;;Readable,writeable,and access-
                            dw 0xCF93
                                         ;;able,unit is 4k.
                            db 0x00

    gl_gdt_sysvga                ;;Vga text mode base address,which can be
                                 ;;used by display driver.
                                 ;;This segment is loaded to gs segment.
                            dw 0x0048    ;;The segment's limit is 0x48 * 4K
                                         ;;= 288K,countains the vag text m-
                                         ;;ode buffer and graphic mode buf-
                                         ;;fer.
                            dw 0x8000
                            db 0x0b      ;;This segment's base address is
                                         ;;0x0b8000,which is the display's
                                         ;;text mode buffer address.
                            dw 0xc093
                            db 0x00

        times 256 dd 0x00        ;;The following is reserved gdt entry spa-
                                 ;;ce,where can be resided by TSS,LDT and
                                 ;;other system tables.
                                 ;;We reserved 4K memory for this usage,so,
                                 ;;it can countains max 512 GDT entry,if all
                                 ;;these entries is used by user applicatio-
                                 ;;n,this operating system can support max
                                 ;;216 user applications(task,or process),
                                 ;;each application use a LDT and a TSS.
        times 256 dd 0x00
        times 256 dd 0x00
        times 256 dd 0x00

align 4
    gl_gdtr_ldr:                 ;;The following variables countains info
                                 ;;about GDT table,which is used by lgdt
                                 ;;instruction.
        dw 256*4*4 + 6*8 - 1     ;;The gdt limit.
        dd gl_sysgdt             ;;The gdt base linear address.

align 8
    gl_sysidt:                   ;;The following area is used by system IDT
                                 ;;(Interrupt Descriptor Table).
                                 ;;The hardware(CPU) can only support 256
                                 ;;interrupts,for each interrupt,there is a
                                 ;;entry,so there should be reserved 2K sp-
                                 ;;ace for the IDT,but in PC archiecture,128
                                 ;;interrupts is enough,so we reserved 128
                                 ;;IDT entries.
        times 128 dd 0x00
        times 128 dd 0x00

    gl_trap_int_handler:         ;;The following array countains the inter-
                                 ;;rupt handlers offset,this array is used
                                 ;;to fill the idt.
                                 ;;We implement the interrupt handlers and
                                 ;;system call procedures in another code
                                 ;;segment,when Hello China initialized,it
                                 ;;load that segment into memory,and then
                                 ;;fill this array using that segment's data.
                                 ;;Hello China declares 128 interrupt handle-
                                 ;;rs,it's enough.
                                 ;;The first 32 IDT entries are trap gate,and
                                 ;;the following entries are interrupt gate,
                                 ;;so,we deal them separately.
                                 ;;When the Hello China's kernal,Master isn't
                                 ;;loaded,the mini-kernal initialize these
                                 ;;handlers using np_inth_tmp and np_traph_tmp.
        dd    gl_traph_tmp_00
        dd    gl_traph_tmp_01
        dd    gl_traph_tmp_02
        dd    gl_traph_tmp_03
        dd    gl_traph_tmp_04
        dd    gl_traph_tmp_05
        dd    gl_traph_tmp_06
        dd    gl_traph_tmp_07
        dd    gl_traph_tmp_08
        dd    gl_traph_tmp_09
        dd    gl_traph_tmp_0a
        dd    gl_traph_tmp_0b
        dd    gl_traph_tmp_0c
        dd    gl_traph_tmp_0d
        dd    gl_traph_tmp_0e
        dd    gl_traph_tmp_0f

        times 16 dd gl_traph_tmp

        dd    np_int20
        dd    np_int21
        dd    np_int22
        dd    np_int23
        dd    np_int24
        dd    np_int25
        dd    np_int26
        dd    np_int27
        dd    np_int28
        dd    np_int29
        dd    np_int2a
        dd    np_int2b
        dd    np_int2c
        dd    np_int2d
        dd    np_int2e
        dd    np_int2f

        times 79 dd np_inth_tmp

        dd    gl_syscall
        

    gl_idtr_ldr:
        dw 8*128 - 1             ;;The IDT table's limit
        dd gl_sysidt             ;;The IDT table's linear address.
                                 ;;This label,gl_idt_ldr,is used by the in-
                                 ;;struction lidt.


;;------------------------------------------------------------------------
;;    The following code is a display(console) driver.
;;------------------------------------------------------------------------
    ;;The following definations are used for console driver.
    ;;We assume that the driver card is VGA compatiable.

    %define DEF_INDEX_PORT 0x3d4 ;;The index port of the display card.
    %define DEF_VALUE_PORT 0x3d5 ;;The data port of the display card.

    %define DEF_DISPMEM_START 0x000b8000  ;;The display memory's start addr.
    %define DEF_DISPMEM_END   0x000bc000  ;;The display memory's end addr.
                                          ;;The display memory's size is 16k.
    %define DEF_REG_CUR_H  0x0e        ;;Cursor's current position's high by-
                                       ;;te register.
    %define DEF_REG_CUR_L  0x0f        ;;Low byte register.

    %define DEF_REG_MEM_H     0x0c     ;;The display memory's original addre-
                                       ;;ss,high byte.
    %define DEF_REG_MEM_L     0x0d     ;;The display memory's original addre-
                                       ;;ss,low byte.

    %define DEF_NUM_COLUMN    80       ;;The columns number of the display.
    %define DEF_NUM_LINE      25       ;;Lines number.

    %define DEF_MAX_STR_LEN   512      ;;The max string's length.

align 8

gl_condrv_data_section:          ;;The following is the data section of con-
                                 ;;sole driver.

    gl_x              dw 0x00    ;;Current columns.
    gl_y              dw 0x00    ;;Current lines.

    gl_dispmem_start  dd DEF_DISPMEM_START
                                 ;;Display memory's start address.

    gl_dispmem_end    dd DEF_DISPMEM_START + 16000
                                 ;;Display memory's end address.
                                 ;;The display memory's size can be deduced
                                 ;;by sub gl_dispmem_start from gl_dispmem_end.
    gl_curr_start     dd 0x000b8000
                                 ;;The current address of the display memory.

    gl_onescrn_size   dd 4000    ;;The one screen's display memory size,in 80
                                 ;;*25 mode,it's 4000 byte.

    gl_default_attr   db 0x07    ;;The default character attribute,white for-
                                 ;;eground,black background,not flash.
                                 ;;This attribute can be changed by applicat-
                                 ;;ion.


np_setcursor:                    ;;The procedure moves the cursor to x,y.
                                 ;;SetCursor(),the cursor's position is
                                 ;;stored at gl_x,gl_y.
    cli                          ;;First,mask all maskable interrupts.
    push ebp
    mov ebp,esp
    push edx
    cmp word [gl_x],DEF_NUM_COLUMN
    jae .ll_error
    cmp word [gl_y],DEF_NUM_LINE
    jae .ll_error
    xor eax,eax
    mov ax,word [gl_y]
    mov dl,DEF_NUM_COLUMN
    mul dl
    add ax,word [gl_x]
    shl eax,0x01
    add eax,dword [gl_curr_start]
    sub eax,DEF_DISPMEM_START         ;;Now,we have get the offset of the cu-
                                      ;;rsor's position releative the start
                                      ;;address of display memory.
    push eax
    push eax
    mov dx,DEF_INDEX_PORT
    mov al,DEF_REG_CUR_L
    out dx,al
    mov dx,DEF_VALUE_PORT
    pop eax
    shr ax,0x01
    out dx,al

    mov dx,DEF_INDEX_PORT
    mov al,DEF_REG_CUR_H
    out dx,al
    mov dx,DEF_VALUE_PORT
    pop eax
    shr ax,0x09

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -