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

📄 miniker.asm

📁 C写的小型操作系统源码
💻 ASM
📖 第 1 页 / 共 5 页
字号:
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000

                    db 0x00       ;;This byte is used for queue head.
                    db 0x00       ;;This byte is used for queue trail.
                    db 0x00       ;;This byte record the elements' counter.

    gl_extendqueue:              ;;This queue is used for extend key buffer.
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000
                    dd 0x00000000

                    db 0x00      ;;Queue head.
                    db 0x00      ;;Queue trial.
                    db 0x00      ;;Elements' counter.

    %define DEF_QUEUE_LEN  0x40  ;;The queue's length,in this implementation,
                                 ;;we assume the key-board buffer queue's le-
                                 ;;ngth is 64 byte.

    gl_notify_os:                ;;This double word countains a procedure,wh-
                                 ;;ich is called by the key-board driver to
                                 ;;notify the os kernal(here,is the master)
                                 ;;this event.
                                 ;;It can be set by a system call,if this
                                 ;;handler is null(0),the driver will not
                                 ;;call it.
                                 ;;NotifyOS(dword control_and_keycode).
                                 ;;Parameter means is following:
                                 ;;bit 0 - 7 : key code.
                                 ;;bit 8 - 13 : the same as bellow.
                                 ;;bit 14 : 0 for key down,1 for key up.
                                 ;;bit 15 : 0 for normal key,1 for extend key.
                                 ;;bit 16 - 31: reserved.

                    ;dd 0x00000000
                    dd np_notify_os  ;;---------- ** debug ** -----------
    gl_control_bits:
                    db 0x00      ;;Control byte,following are it's means:
                                 ;;bit 0 : Shift key status,1 = shift down.
                                 ;;bit 1 : alt key status,1 = alt key down.
                                 ;;bit 2 : ctrl key status,1 = ctrl key down.
                                 ;;bit 3 : caps lock key status,1 = down.
                                 ;;bit 4 : e0 byte status,if the first scan-
                                 ;;        code is e0,then this bit set.
                                 ;;bit 5 : e1 byte status.
                                 ;;bit 6 : reserved.
                                 ;;bit 7 : reserved.


    gl_scanasc_map:              ;;The following is a scancode to ascii code
                                 ;;map table,used by the key board driver
                                 ;;to find the ascii code according the scan-
                                 ;;code.
    db 0x00
    db VK_ESC,           '1',            '2',            '3',            '4'
    db '5',              '6',            '7',            '8',            '9'
    db '0',              '-',            '=',            VK_BACKSPACE,   VK_TAB
    db 'q',              'w',            'e',            'r',            't'
    db 'y',              'u',            'i',            'o',            'p'
    db '[',              ']',            VK_RETURN,      VK_LEFT_CTRL,   'a'
    db 's',              'd',            'f',            'g',            'h'
    db 'j',              'k',            'l',            ';',            39  ;;'''
    db '`',              VK_LEFT_SHIFT,  '\',            'z',            'x'
    db 'c',              'v',            'b',            'n',            'm'
    db ',',              VK_DOT,         '/',            VK_RIGHT_SHIFT, '*'
    db VK_LEFT_ALT,      VK_SPACE,       VK_CAPS_LOCK,   VK_F1,          VK_F2
    db VK_F3,            VK_F4,          VK_F5,          VK_F6,          VK_F7
    db VK_F8,            VK_F9,          VK_F10,         VK_KP_NUM,      VK_KP_SCROLL
    db VK_KP_7,          VK_KP_8,        VK_KP_9,        VK_KP_MINIS_SIGN, VK_KP_4
    db VK_KP_5,          VK_KP_6,        VK_KP_ADD_SIGN, VK_KP_1,        VK_KP_2
    db VK_KP_3,          VK_KP_0,        VK_KP_DOT,      0x00,           0x00
    db 0x00,             VK_F11,         VK_F12,         0x00,           0x00

    gl_scanasc_map_up:           ;;The following is a scancode to ascii code
                                 ;;map table,used by the key board driver
                                 ;;to find the ascii code according the scan-
                                 ;;code.
    db 0x00
    db VK_ESC,           '!',            '@',            '#',            '$'
    db '%',              '^',            '&',            '*',            '('
    db ')',              '_',            '+',            VK_BACKSPACE,   VK_TAB
    db 'Q',              'W',            'E',            'R',            'T'
    db 'Y',              'U',            'I',            'O',            'P'
    db '{',              '}',            VK_RETURN,      VK_LEFT_CTRL,   'A'
    db 'S',              'D',            'F',            'G',            'H'
    db 'J',              'K',            'L',            ':',            '"'
    db '~',              VK_LEFT_SHIFT,  '|',            'Z',            'X'
    db 'C',              'V',            'B',            'N',            'M'
    db '<',              '>',            '?',            VK_RIGHT_SHIFT, '*'
    db VK_LEFT_ALT,      VK_SPACE,       VK_CAPS_LOCK,   VK_F1,          VK_F2
    db VK_F3,            VK_F4,          VK_F5,          VK_F6,          VK_F7
    db VK_F8,            VK_F9,          VK_F10,         VK_KP_NUM,      VK_KP_SCROLL
    db VK_KP_7,          VK_KP_8,        VK_KP_9,        VK_KP_MINIS_SIGN, VK_KP_4
    db VK_KP_5,          VK_KP_6,        VK_KP_ADD_SIGN, VK_KP_1,        VK_KP_2
    db VK_KP_3,          VK_KP_0,        VK_KP_DOT,      0x00,           0x00
    db 0x00,             VK_F11,         VK_F12,         0x00,           0x00

    gl_handler_table:            ;;The following table countains the key pre-
                                 ;;ss event handlers.
    dd np_null_handler,           np_null_handler   ;;VK_ESC
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_null_handler   ;;VK_TAB
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_null_handler   ;;VK_LEFT_CTRL
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_shift_handler
    dd np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_asckey_handler,         np_asckey_handler
    dd np_shift_handler
    dd np_asckey_handler
    dd np_null_handler                              ;;VK_LEFT_ALT
    dd np_asckey_handler
    dd np_capslock_handler
    times 64 dd np_null_handler  ;;--------------- ** debug ** -----------
    dd VK_F1
    dd VK_F2,                     VK_F3
    dd VK_F4,                     VK_F5
    dd VK_F6,                     VK_F7
    dd VK_F8,                     VK_F9
    dd VK_F10,                    VK_KP_NUM
    dd VK_KP_SCROLL,              VK_KP_7
    dd VK_KP_8,                   VK_KP_9
    dd VK_KP_MINIS_SIGN,          VK_KP_4
    dd VK_KP_5,                   VK_KP_6
    dd VK_KP_ADD_SIGN,            VK_KP_1
    dd VK_KP_2,                   VK_KP_3
    dd VK_KP_0,                   VK_KP_DOT
    dd 0x00,                      0x00
    dd 0x00,                      VK_F11
    dd VK_F12,                    0x00
    dd 0x00,                      0x00

np_notify_os:                    ;;------------ ** debug ** --------------
    push ebp
    mov ebp,esp
    push ebx
    mov eax,dword [ebp + 0x08]
    test ah,01000000b
    jnz .ll_end                  ;;Key up,do nothing.
    test ah,10000000b
    jnz .ll_end                  ;;Extend key,do nothing.
    mov ebx,gl_normalqueue
    push ebx
    call np_dequeue
    pop ebx
    cmp eax,DEF_FALSE
    je .ll_end
    cmp al,VK_RETURN
    jne .ll_continue
    call np_changeline
    call np_gotohome
    jmp .ll_end
.ll_continue:
    cmp al,VK_BACKSPACE
    je .ll_continue_1
    jmp .ll_printkey
.ll_continue_1:
    call np_gotoprev
    jmp .ll_end
.ll_printkey:
    mov ah,0x07
    push eax
    call np_printch
    pop eax
.ll_end:
    pop ebx
    leave
    retn                         ;;End of the procedure.

np_null_handler:                 ;;This handler is a null handler,to fill up
                                 ;;the handler table,and may be updated in the
                                 ;;future.
    push ebp
    mov ebp,esp
    leave
    retn                         ;;End of the procedure.

np_capslock_handler:             ;;This procedure handles the caps-lock key
                                 ;;up/down events.
    push ebp
    mov ebp,esp
    push ebx
    mov eax,dword [ebp + 0x08]
    cmp al,0x80
    jae .ll_keyup
    mov ah,byte [gl_control_bits]
    test ah,00001000b
    jz .ll_set1
    and ah,11110111b             ;;Clear the caps-lock key bit.
    mov byte [gl_control_bits],ah  ;;Update the control byte.
    jmp .ll_continue
.ll_set1:
    or ah,00001000b              ;;Set the caps-lock key down bit.
    mov byte [gl_control_bits],ah
.ll_continue:
    or ah,10000000b              ;;Set the extend key bit.
    mov ebx,dword [gl_notify_os]
    cmp ebx,0x00000000
    je .ll_end
    push eax
    call ebx
    pop eax
    jmp .ll_end
.ll_keyup                        ;;If this is a key down event,only notify
                                 ;;os kernal this event,do not reset the
                                 ;;caps-lock control bit.
    mov ah,byte [gl_control_bits]
    or ah,11000000b              ;;Set the extend key bit and the key up bit.
    sub al,0x80
    mov ebx,dword [gl_notify_os]
    cmp ebx,0x00000000
    je .ll_end
    push eax
    call ebx
    pop eax
.ll_end:
    pop ebx
    leave
    retn                         ;;End of the procedure.

np_shift_handler:                ;;This procedure handles the shift key up/down
                                 ;;event.
    push ebp
    mov ebp,esp
    push ebx
    xor eax,eax
    mov al,byte [ebp + 0x08]     ;;Get the key's scan code.
    cmp al,0x80
    jae .ll_keyup
    mov ah,byte [gl_control_bits]
    or ah,0x01                   ;;Set the shift down bit.
    mov byte [gl_control_bits],ah
    or ah,10000000b              ;;Set the extend key bit.
    mov ebx,dword [gl_notify_os]
    cmp ebx,0x00000000
    je .ll_end
    push eax
    call ebx                     ;;Notify os kernal this event.
    pop eax
    jmp .ll_end
.ll_keyup:
    mov ah,byte [gl_control_bits]
    and ah,11111110b             ;;Clear the shift down bit.
    mov byte [gl_control_bits],ah
    or ah,11000000b              ;;Set the key up bit and the extend key bit.
    mov ebx,dword [gl_notify_os]
    cmp ebx,0x00000000
    je .ll_end
    sub al,0x80                  ;;Adjust the key's scan code.
    push eax
    call ebx
    pop eax
    jmp .ll_end
.ll_end:
    pop ebx
    leave
    retn                         ;;End of the procedure.

np_general_handler:              ;;This procedure handles all key press events,
                                 ;;and call the correct handler according the
                                 ;;scan code.
                                 ;;The parameter is key's scan code.
    push ebp
    mov ebp,esp
    push ebx
    push esi
    xor ebx,ebx
    xor esi,esi
    mov al,byte [ebp + 0x08]     ;;Get the key's scan code.
    cmp al,0xe0
    je .ll_e0
    cmp al,0xe1
    je .ll_e1
    cmp al,0x80
    jae .ll_keyup                ;;This is a key up event.
    cmp al,0x58                  ;;------------ ** debug ** --------------
    jae .ll_end                  ;;------------ ** debug ** --------------
    mov bl,al
    jmp .ll_continue
.ll_keyup:
    mov bl,al
    sub bl,0x80
.ll_continue:
    mov esi,ebx
    mov ebx,gl_handler_table
    shl esi,0x02
    add ebx,esi
    push eax
    call [ebx]     
    pop eax
    jmp .ll_end
.ll_e0:
    mov ah,byte [gl_control_bits]
    or ah,00010000b
    mov byte [gl_control_bits],ah  ;;Set the e0 bit in control byte.
    jmp .ll_end
.ll_e1:
    mov ah,byte [gl_control_bits]
    or ah,00100000b
    mov byte [gl_control_bits],ah  ;;Set the e1 bit in control byte.
.ll_end:
    pop esi
    pop ebx
    leave
    retn                         ;;The end of the procedure.
    


np_asckey_handler:               ;;The procedure handle the ascii key press
                                 ;;event.
                                 ;;It's parameter is the scan code.
    push ebp
    mov ebp,esp
    push ebx
    xor eax,eax
    mov al,byte [ebp + 0x08]
    cmp al,0x80
    jae .ll_keyup                ;;This event is a key up event.
    mov ah,byte [gl_control_bits]
    test ah,00000001b            ;;If shift key down.
    jnz .ll_shiftdown
    test ah,00001000b            ;;If capslock key down.
    jnz .ll_capslockdown
    mov ebx,gl_scanasc_map
    jmp .ll_continue
.ll_shiftdown:
.ll_capslockdown:
    mov ebx,gl_scanasc_map_up
.ll_continue:
    xor eax,eax
    mov al,byte [ebp + 0x08]
    add ebx,eax
    mov al,byte [ebx]
    push eax
    push gl_normalqueue
    call np_inqueue              ;;Add the key into normal key queue.
    pop eax
    pop eax

⌨️ 快捷键说明

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