asm汇编源代码之按键处理相关函数

提供5个子程序:

1. 发送按键 sendkey

2. 检测是否有按键 testkey

3. 读取按键 getkey

4. 判断键盘缓冲区是否为空 bufempty

5. 判断键盘缓冲区是否已满 buffull

具体功能及参数描述如下

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| sendkey proc  far ; ax=charcode testkey proc  far ; out: ;   zf=1 buff empty; ;   zf=0 ax=key code getkey proc  far ; out: ;   ax=key code ; if buf empty, it will wait bufempty proc  far  ; test key buffer be empty or not ; out: ;   zf=0 not empty;  zf=1 empty buffull proc  far  ; test key buffer be full or not ; out: ;   zf=0 not full;  zf=1 full   |

复制代码
; more source code at http://www.ahjoe.com/source/srcdown.asp

.286
        public  sendkey, getkey, testkey, buffull, bufempty
code    segment
        assume  cs:code, ds:code

sendkey proc    far
;  ax=charcode
        push    ax
        push    bx
        push    si
        push    di
        push    ds
        cmp     ah, 0
        jnz     sendkj0
        mov     ah, 30h
sendkj0:
        mov     bx, 40h
        mov     ds, bx
        mov     di, ds:[1ch]

        mov     bx, di
        call    incptr
        cmp     bx, ds:[1ah]
        jz      fullk

        mov     ds:[di], ax
        mov     ds:[1ch], bx
fullk:
        pop     ds
        pop     di
        pop     si
        pop     bx
        pop     ax
        retf
sendkey endp                              

getkey  proc    far
; out:
;     ax=key code
; if buf empty, it will wait

        push    bx
        push    si
        push    ds

        mov     ax, 40h
        mov     ds, ax
getkeyrep:
        mov     si, ds:[1ah]
        cmp     si, ds:[1ch]
        jz      getkeyrep
        mov     ax, [si]
        mov     bx, si
        call    incptr
        mov     ds:[1ah], bx
        pop     ds
        pop     si
        pop     bx
        retf	
getkey  endp

testkey proc    far
; out:
;      zf=1  buff empty;
;      zf=0  ax=key code
        push    si
        push    ds

        mov     ax, 40h
        mov     ds, ax
        mov     si, ds:[1ah]
        mov     ax, [si]
        cmp     si, ds:[1ch]

        pop     ds
        pop     si
        retf    
testkey endp


bufempty proc    far    ; test key buffer be empty or not
; out:
;      zf=0  not empty;   zf=1  empty
        push    si
        push    ds

        mov     si, 40h
        mov     ds, si
        mov     si, ds:[1ah]
        cmp     si, ds:[1ch]

        pop     ds
        pop     si
        retf
bufempty endp

buffull  proc    far    ; test key buffer be full or not
; out:
;      zf=0  not full;    zf=1  full
        push    ds
        push    bx
        mov     bx, 40h
        mov     ds, bx
        mov     bx, ds:[1ch]
        call    incptr
        cmp     bx, ds:[1ah]
        pop     bx
        pop     ds
        retf
buffull  endp

incptr  proc    near
        inc     bx
        inc     bx
        cmp     bx, 3eh
        jb      incok
        mov     bx, 1eh
incok:
        retn
incptr  endp

decptr  proc    near
        dec     bx
        dec     bx
        cmp     bx, 1ch
        ja      decok
        mov     bx, 3ch
decok:
        retn
decptr  endp

code    ends
        end     sendkey
相关推荐
liulilittle15 小时前
C++ i386/AMD64平台汇编指令对齐长度获取实现
c语言·开发语言·汇编·c++
工业互联网专业16 小时前
汇编与接口技术:8259中断实验
汇编·单片机·嵌入式硬件·8259中断实验
small_wh1te_coder1 天前
硬件嵌入式学习路线大总结(一):C语言与linux。内功心法——从入门到精通,彻底打通你的任督二脉!
linux·c语言·汇编·嵌入式硬件·算法·c
Ronin-Lotus13 天前
微处理器原理与应用篇---ARM常见汇编指令
汇编·arm开发·微处理原理与应用
永夜的黎明16 天前
【二进制安全作业】250616课上作业1-栈溢出漏洞利用
c语言·汇编·安全
Geometry Fu19 天前
物联网控制技术 知识点总结 第三章 汇编语言 第四章 C51语言
汇编·物联网·51单片机
半桔19 天前
【Linux手册】进程的状态:从创建到消亡的“生命百态”
linux·运维·服务器·汇编·深度学习·面试
一条叫做nemo的鱼22 天前
从汇编的角度揭开C++ this指针的神秘面纱(下)
java·汇编·c++·函数调用·参数传递
一条叫做nemo的鱼22 天前
从汇编的角度揭开C++ this指针的神秘面纱(上)
汇编·c++·算法·函数调用·this指针·参数传递
qwertyuiop_i24 天前
汇编(函数调用)
汇编·windows·函数调用