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
相关推荐
hef2881 小时前
NASM工具怎么用 汇编转机器码实战教程
汇编
是星辰吖~8 小时前
X86反汇编:内存幻影_数组解码纪元(3-2)
汇编
是星辰吖~11 小时前
X86反汇编:内存矩阵与指针之剑(3-1)
汇编
iCxhust1 天前
如何利用iret修改cs ip
汇编·单片机·嵌入式硬件·微机原理·8088单板机
是星辰吖~2 天前
X86反汇编:透视之眼_反编译特训(1-2)
汇编
是星辰吖~3 天前
X86反汇编:破茧成蝶 —— 赤裸逻辑与机械之心(1-1)
汇编
逆向命运3 天前
PC企微搜索手机号窗口绕过
c语言·汇编·c++·飞书·企业微信
是星辰吖~4 天前
函数战争:内存领地的争夺与撤退
汇编
止观止4 天前
在 WSL2 上从零搭建 ARM 混合编程环境
汇编·arm开发·嵌入式开发·混合编程
say_fall5 天前
8086汇编程序设计_从基础到实战
开发语言·汇编·8086