How to check keys status in x86 assembly?

后端 未结 3 1607
暖寄归人
暖寄归人 2020-12-11 17:40

I took x86 assembly as a hobby this past january so I could make games that would work on old 8086-powered computers like the PCj and Tandy 1000, but the books I found don\'

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-11 18:16

    Example of polling the keyboard using port 60h and port 64h:

           cli            ; stop software-Interrupts
           mov  al, 2     ; stop IRQ 1
           out  21h, al
           sti
    P1:
           in   al, 64h     ; get Status
           test al, 1       ;  is there something in the outputbuffer?
           jz  P1
           test al, 20h     ;  it is a byte from the PS2-Mouse?
           jnz  P1
           in   al, 60h     ; get a key
           cmp  al, 1       ;  Escape-key?
           jz  XRAUS        ; then goto end
    ;───────────────────────────────────────────────────────────────
           mov  si, OFFSET SONTAB  ; get the offsetaddress of our special-key table
           mov  cl, Extablen       ; lenght
    XSUCH: cmp  al, [si]
           jz  XFOUND
           lea  si, [si+1]         ; instead of "inc si"
           dec  cl
           jnz XSUCH
    ;───────────────────────────────────────────────────────────────
           mov  si, OFFSET TASTTAB  ; get the offsetaddress of our key table
           mov  cx, tablen
           mov  bx, OFFSET TEXTTAB  ; our corresponding ASCII table
    SUCH:  cmp  al, [si]
           jz  short FOUND
           lea  si, [si+1]
           dec  cx
           jnz SUCH
           jmp  P1
    ;───────────────────────────────────────────────────────────────
    XRAUS: in   al, 60h ; clear outputbuffer
           cli
           xor  al, al  ; enable IRQ 1
           out  21h, al
           sti
           mov  ah, 1  ; clear buffer in the ram
           int  16h
    ; ...some more instructions
    ;───────────────────────────────────────────────────────────────
    FOUND:  mov  si, tablen   ; Length
            sub  si, cx
            xor  ecx, ecx
            mov  cl, [bx+si]  ; get the ASCII from our table
    ;  ...some more instructions
    ;───────────────────────────────────────────────────────────────
    XFOUND:
    ;  Tab,shift li.,shift re.,HOME,UP,LEFT,RIGHT,END,DOWN
            cmp  cl, 1       ; DOWN-key
            jnz short  ...   ; jump to next
            ....
            ....
            cmp  cl, 9       ; Tab-key
            jnz  P1
    ;  ...some more instructions
    :------------------------Data area----------------------
    TASTTAB DB 02h,03h,04h,05h,06h,07h,08h,09h,0Ah,0Bh,0Ch,0Dh
            DB 10h,11h,12h,13h,14h,15h,16h,17h,18h,19h,1Ah,1Bh,1Eh,1Fh
            DB 20h,21h,22h,23h,24h,25h,26h,27h,28h,29h,2Bh,2Ch,2Dh,2Eh,2Fh
            DB 30h,31h,32h,33h,34h,35h,39h
            DB 56h
    tablen =  ($-TASTTAB)
    TEXTTAB DB "1234567890ß'"     ; with some german letters inside
            DB "qwertzuiopü+as"
            DB "dfghjklöä^#yxcv"
            DB "bnm,.- "
            DB "<"
    Textablen  =  ($-TEXTTAB)
    ;---------------------------------------------------------------------------
    ;  Tab,shift left.,shift rigth.,HOME,UP,LEFT,RIGHT,END,DOWN
    ;----------
    SONTAB  DB 0Fh,2Ah,36h,47h,48h,4Bh,4Dh,4Fh,50h
    Extablen  =  ($-SONTAB)
            DB 0,0,0 ; for data_alignment of following entries
    

提交回复
热议问题