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

📄 miniker.asm

📁 C写的小型操作系统源码
💻 ASM
📖 第 1 页 / 共 5 页
字号:
    mov ebx,dword [gl_notify_os]
    cmp ebx,0x00000000
    jz .ll_end
    mov ah,byte [gl_control_bits]
    push eax
    call ebx                     ;;Notify the os kernal this event.
    pop eax
    jmp .ll_end
.ll_keyup:
    mov ebx,dword [gl_notify_os]
    cmp ebx,0x00000000
    jz .ll_end
    mov ah,byte [gl_control_bits]
    or ah,01000000b              ;;Set the key up bit.
    push eax
    call ebx                     ;;Notify the os kernal this event.
    pop eax
.ll_end:
    pop ebx
    leave
    retn



np_inqueue:                      ;;This procedure add a element into the qu-
                                 ;;eue,and update the current queue control
                                 ;;value.
                                 ;;The parameter is queue's base address,and
                                 ;;the element to be added to the queue.
                                 ;;InQueue(dword dwQueueBase,byte ele).
    push ebp
    mov ebp,esp
    push ebx
    push ecx
    mov ebx,dword [ebp + 0x08]   ;;Get the current queue's base address.
    push ebx
    call np_queuefull
    pop ebx
    cmp eax,DEF_TRUE
    je .ll_queuefull             ;;If the current queue is full,only set the
                                 ;;return result to false,and return.
    xor eax,eax
    mov al,byte [ebx + DEF_QUEUE_LEN + 1]  ;;Get the current queue's trial.
    mov cl,byte [ebp + 0x0c]     ;;Get the element to be added to the queue.
    mov byte [ebx + eax],cl      ;;Add the element to the queue.
    inc al
    cmp al,DEF_QUEUE_LEN
    je .ll_adjust
    jmp .ll_continue
.ll_adjust:
    mov al,0x00
.ll_continue:
    inc byte [ebx + DEF_QUEUE_LEN + 2]  ;;Update the queue element's counter.
    mov byte [ebx + DEF_QUEUE_LEN + 1],al ;;Update the current queue's trial.
    mov eax,DEF_TRUE
    jmp .ll_end
.ll_queuefull:
    mov eax,DEF_FALSE
.ll_end:
    pop ecx
    pop ebx
    leave
    ret                          ;;End of the procedure.


np_dequeue:                      ;;This procedure delete a element from the
                                 ;;queue,and update the current queue control
                                 ;;value.
                                 ;;The parameter is queue's base address,and
                                 ;;the return value is countained in eax re-
                                 ;;gister,which al countains the element get
                                 ;;from the queue.
    push ebp
    mov ebp,esp
    push ebx
    push ecx
    mov ebx,dword [ebp + 0x08]   ;;Get the current queue's base address.
    push ebx
    call np_queueempty
    pop ebx
    cmp eax,DEF_TRUE
    je .ll_queueempty
    xor eax,eax
    mov al,byte [ebx + DEF_QUEUE_LEN]  ;;Get the queue's head.
    mov cl,byte [ebx + eax]      ;;Get the current element.
    inc al
    cmp al,DEF_QUEUE_LEN
    je .ll_adjust
    jmp .ll_continue
.ll_adjust:
    mov al,0x00
.ll_continue:
    mov byte [ebx + DEF_QUEUE_LEN],al  ;;Update the queue's head value.
    dec byte [ebx + DEF_QUEUE_LEN + 2] ;;Update the queue's length.
    xor eax,eax
    mov al,cl
    jmp .ll_end
.ll_queueempty:                  ;;If the current queue is full,this proced-
                                 ;;ure only set the return value to false,and
                                 ;;return.
    mov eax,DEF_FALSE
.ll_end:
    pop ecx
    pop ebx
    leave
    ret                          ;;End of the procedure.

np_queuefull:                    ;;Determine if the current queue is full.
                                 ;;If the queue's trail + 1 = queue's head,
                                 ;;the queue is full.
                                 ;;The parameter is queue's base address.
    push ebp
    mov ebp,esp
    mov eax,dword [ebp + 0x08]   ;;Get the queue's base address.
    add eax,DEF_QUEUE_LEN
    add eax,0x02                 ;;Adjust the eax register,so this register
                                 ;;countains the queue element counter's of-
                                 ;;fset.
    cmp byte [eax],DEF_QUEUE_LEN
    je .ll_full
    mov eax,DEF_FALSE
    jmp .ll_end
.ll_full:
    mov eax,DEF_TRUE
.ll_end:
    leave
    ret                          ;;End of the procedure.

np_queueempty:                   ;;Determine if the current queue is empty.
                                 ;;If the queue's head = queue's trial,then
                                 ;;the queue is empty.
                                 ;;The parameter is queue's base address.
    push ebp
    mov ebp,esp
    mov eax,dword [ebp + 0x08]
    add eax,DEF_QUEUE_LEN
    add eax,0x02
    cmp byte [eax],0x00
    je .ll_empty
    mov eax,DEF_FALSE
    jmp .ll_end
.ll_empty:
    mov eax,DEF_TRUE
.ll_end:
    leave
    ret                          ;;End of the procedure.

np_set_notifyos_handler:         ;;This procedure set the notify os handler.
                                 ;;It's parameter is the handler's base ad-
                                 ;;dress.This procedure returns the previous
                                 ;;handler's base address.
                                 ;;dword SetNotifyOsHandler(dword dwBaseAddr).
    push ebp
    mov ebp,esp
    push ebx
    mov eax,dword [ebp + 0x08]   ;;Get the handler's base address.
    cmp eax,0x00000000
    jz .ll_error
    mov ebx,dword [gl_notify_os]
    mov dword [gl_notify_os],eax
    mov eax,ebx                  ;;Set the return value to the previous hand-
                                 ;;ler's value.
    jmp .ll_end
.ll_error:                       ;;If the parameter is null,then return a
                                 ;;false.
    mov eax,DEF_FALSE
.ll_end:
    pop ebx
    leave
    retn                         ;;End of the procedure.

;;---------------- ** System initialize code section ** ------------------
;;  The following section countains the system initialize code,these code
;;  Initialize the CPU running context,such as GDT,IDT,and some system le-
;;  vel arrays.
;;------------------------------------------------------------------------

align 4
gl_sysredirect:                  ;;Redirect code of mini-kenal,this code
                                 ;;moves the mini-kernal from con_org_st-
                                 ;;art_addr to con_start_addr.
    mov ecx,con_mini_size + con_mast_size
    shr ecx,0x02
    mov esi,con_org_start_addr   ;;Original address.
    mov edi,con_start_addr       ;;Target address.
    cld
    rep movsd

    mov eax,gl_initgdt
    jmp eax                      ;;After moved mini-kernal to the start
                                 ;;address,the mini-kernal then jump to
                                 ;;the start entry,labeled by gl_initgdt.

gl_initgdt:                      ;;The following code initializes the GDT
                                 ;;and all of the segment registers.
    lgdt [gl_gdtr_ldr]           ;;Load the new gdt content into gdt regis-
                                 ;;ter.
    mov ax,0x010
    mov ds,ax
    mov ax,0x018
    mov ss,ax
    mov esp,DEF_INIT_ESP         ;;The two instructions,mov ss and mov esp
                                 ;;must resides together.
    mov ax,0x020
    mov es,ax
    mov fs,ax                    ;;Initialize the fs as the same content as
                                 ;;es.If need,we can change the fs's value.
    mov ax,0x020
    mov gs,ax
    jmp dword 0x08 : gl_sysinit  ;;A far jump,to renew the cs register's v-
                                 ;;alue,and clear the CPU's prefetched que-
                                 ;;ue,trans the control to the new squence.

gl_sysinit:                      ;;The start position of the init process.
    mov eax,gl_trap_int_handler
    push eax
    call np_fill_idt             ;;Initialize the IDT table.
    pop eax
    lidt [gl_idtr_ldr]           ;;Load the idtr.
    call np_init8259             ;;Reinitialize the interrupt controller.
    sti
    nop
    nop
    nop
    mov eax,con_mast_start     
    jmp eax

;; The following defines some helper procedures,to help the system initial-
;; ize process.

np_fill_idt:                     ;;This procedure fills the IDT table.
    push ebp
    mov ebp,esp

    %define DEF_TRAP_WORD_0 0x0000
    %define DEF_TRAP_WORD_1 0x0008
    %define DEF_TRAP_WORD_2 0x8f00
    %define DEF_TRAP_WORD_3 0x0000

    %define DEF_INT_WORD_0  0x0000
    %define DEF_INT_WORD_1  0x0008
    %define DEF_INT_WORD_2  0x8e00
    %define DEF_INT_WORD_3  0x0000

    push ecx
    push esi
    push edi
    push ebx
    mov esi,DEF_PARA_01
    mov edi,gl_sysidt
    cld
    mov ecx,32                   ;;Init the first 32 entries of IDT table.
.ll_bgn1:
    mov eax,DEF_TRAP_WORD_1
    shl eax,0x010
    mov ebx,dword [esi]
    mov ax,bx
    stosd
    shr ebx,0x010
    mov ax,bx
    shl eax,0x010
    mov ax,DEF_TRAP_WORD_2
    stosd
    add esi,0x04
    loop .ll_bgn1

    mov ecx,96                   ;;Initialize the rest 96 entries of IDT.
.ll_bgn2:
    mov eax,DEF_INT_WORD_1
    shl eax,0x010
    mov ebx,dword [esi]
    mov ax,bx
    stosd
    shr ebx,0x010
    mov ax,bx
    shl eax,0x010
    mov ax,DEF_INT_WORD_2
    stosd
    add esi,0x04
    loop .ll_bgn2
    pop ebx
    pop edi
    pop esi
    pop ecx
    leave
    ret                          ;;End of the procedure.

np_init8259:                     ;;This procedure initializes the int-
                                 ;;errupt controller,8259 chip.
    mov al,0x11
    out 0x20,al
    nop
    nop
    out 0xa0,al

    mov al,0x20
    out 0x21,al
    mov al,0x28
    out 0xa1,al

    mov al,4
    out 0x21,al
    mov al,2
    out 0xa1,al

    mov al,1
    out 0x21,al
    out 0xa1,al

    mov al,0x00
    out 0x21,al
    mov al,0x00
    out 0xa1,al
    ret                         ;;End of the procedure.


;;------------------------- ** Temp procedures ** ------------------------
;;  The following defines some temp procedures,are used by the mini-kernal
;;  to fill the system tables.

np_delay:
    push ebp
    mov ebp,esp
    sub esp,0x04
    mov word [ebp - 2],0x00ff
.ll_begin:
    mov word [ebp - 4],0xffff 
.ll_loop:
    nop
    nop
    nop
    nop
    dec word [ebp - 4]
    jnz .ll_loop
    dec word [ebp - 2]
    jnz .ll_begin

    add esp,0x04
    leave
    ret                          ;;End of the procedure.


np_traph_tmp:                    ;;This procedure is used to fill the first
                                 ;;32 entries of IDT table.
                                 ;;After the Master kernal is loaded,it wou-
                                 ;;ld replaced by others.
    push eax
    ;call np_formatdbgstr         ;;This procedure only print out somethings
                                 ;;then returned.
    ;call np_dbg_output
    mov al,0x20                  ;;Indicate the interrupt chip we have fin-
                                 ;;ished handle the interrupt.
                                 ;;:-)
    out 0x20,al
    out 0xa0,al
    pop eax
    pop eax
    iret                         ;;End of the procedure.

gl_traph_tmp:
    push eax
    cmp dword [gl_general_int_handler],0x00000000
    jz .ll_continue
    push ebx                     ;;The following code saves the general
                                 ;;registers.
    push ecx
    push edx
    push esi
    push edi
    push ebp
    mov eax,esp
    push eax
    mov eax,0x1F
    push eax
    call dword [gl_general_int_handler]
    pop eax                      ;;Restore the general registers.
    pop eax
    mov esp,eax
    pop ebp

⌨️ 快捷键说明

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