x86基础实验-INT16读取按键

简介

分别使用AH=0中断方式、AH=1轮询方式读取键盘按键,并输出按键的扫描码和ASCII码。ESC键退出程序

程序运行结果
image.png
代码
  1. AH=0中断方式
ASSUME  CS:CODE1,DS:DATA1,SS:STACK1

STACK1  SEGMENT STACK
STACK_AREA      DW      200H DUP(?)
STACK_BTM       EQU     $-STACK_AREA
STACK1          ENDS

DATA1   SEGMENT

EXPLAIN_MODE_SELECT     DB  13,10,"please select mode:1.read key from keyboard. 2.read key from buffer. other.quit",13,10,'$'
EXPLAIN_INPUT     DB  13,10,"please press any key:",13,10,'$'
EXPLAIN_OUTPUT_SCAN    DB  13,10,"scan code:",'$'
EXPLAIN_OUTPUT_ASCII    DB  13,10,"ascii code:",'$'
ASCII   DB  20H DUP(0)

DATA1   ENDS

CODE1   SEGMENT

MAIN    PROC    NEAR
        MOV     AX,STACK1
        MOV     SS,AX
        MOV     SP,STACK_BTM
        MOV     AX,DATA1
        MOV     DS,AX
        MOV     ES,AX

        MOV     AH,09H
        MOV     DX,OFFSET EXPLAIN_INPUT 
        INT     21H

    MAIN_LOOP_1:

        MOV     AH,0    ;中断方式读取按键
        INT     16H

        MOV     CL,AL
        XOR     CH,CH

        MOV     BL,AH
        XOR     BH,BH

        MOV     AH,09H
        MOV     DX,OFFSET EXPLAIN_OUTPUT_ASCII
        INT     21H

        PUSH    CX 
        CALL    OUTPUT_NUM_DEC
        
        MOV     AH,09H
        MOV     DX,OFFSET EXPLAIN_OUTPUT_SCAN
        INT     21H

        PUSH    BX 
        CALL    OUTPUT_NUM_DEC

        CMP     BX,1
        JNZ     MAIN_LOOP_1

        MOV     AX,4C00H
        INT     21H


    
MAIN    ENDP

OUTPUT_NUM_DEC  PROC    NEAR ;[SP+2]:num

        PUSH    BP
        MOV     BP,SP
        PUSH    SI
        PUSH    DI
        PUSH    AX
        PUSH    BX
        PUSH    CX
        PUSH    DX

        MOV     AX,[BP+4]
        MOV     SI,10
        MOV     DI,0
        MOV     BP,OFFSET ASCII

    HEX2DEC_LOOP2:

        MOV     DX,0

        MOV     CX,10 
        DIV     CX

        MOV     DS:[BP+DI],DL 
        INC     DI
        CMP     AX,0
        JNE     HEX2DEC_LOOP2

        MOV     CX,DI

    HEX2DEX_LOOP3:

        DEC     DI  
        MOV     DL,DS:[BP+DI]

        PUSH    DX
        CALL    OUTPUT_NUM

        LOOP    HEX2DEX_LOOP3

        POP     DX
        POP     CX
        POP     BX
        POP     AX
        POP     DI
        POP     SI
        POP     BP

        RET     2
        

OUTPUT_NUM_DEC  ENDP

OUTPUT_NUM  PROC    NEAR    ;[SP+2]:num(byte)
        PUSH    BP
        MOV     BP,SP
        PUSH    AX
        PUSH    DX

        MOV     DL,[BP+4]
        CMP     DL,9
        JA      OUTPUT_NUM_ALPHA 

        ADD     DL,'0'
        JMP     OUTPUT_NUM_OUTPUT
    OUTPUT_NUM_ALPHA:
        ADD     DL,'A'-10
        
    OUTPUT_NUM_OUTPUT:
        MOV     AH,02H
        INT     21H

        POP     DX
        POP     AX
        POP     BP

        RET     2
OUTPUT_NUM  ENDP

CODE1   ENDS

END     MAIN

  1. AH=1轮询方式
ASSUME  CS:CODE1,DS:DATA1,SS:STACK1

STACK1  SEGMENT STACK
STACK_AREA      DW      200H DUP(?)
STACK_BTM       EQU     $-STACK_AREA
STACK1          ENDS

DATA1   SEGMENT

EXPLAIN_MODE_SELECT     DB  13,10,"please select mode:1.read key from keyboard. 2.read key from buffer. other.quit",13,10,'$'
EXPLAIN_INPUT     DB  13,10,"please press any key:",13,10,'$'
EXPLAIN_OUTPUT_SCAN    DB  13,10,"scan code:",'$'
EXPLAIN_OUTPUT_ASCII    DB  13,10,"ascii code:",'$'
ASCII   DB  20H DUP(0)

DATA1   ENDS

CODE1   SEGMENT

MAIN    PROC    NEAR
        MOV     AX,STACK1
        MOV     SS,AX
        MOV     SP,STACK_BTM
        MOV     AX,DATA1
        MOV     DS,AX
        MOV     ES,AX

        MOV     AH,09H
        MOV     DX,OFFSET EXPLAIN_INPUT 
        INT     21H

    MAIN_LOOP_1:

        TRY_READ:      ;轮询方式读取按键
        MOV     AH,0
        INT     16H
        JZ      TRY_READ

        MOV     CL,AL
        XOR     CH,CH

        MOV     BL,AH
        XOR     BH,BH

        MOV     AH,09H
        MOV     DX,OFFSET EXPLAIN_OUTPUT_ASCII
        INT     21H

        PUSH    CX 
        CALL    OUTPUT_NUM_DEC
        
        MOV     AH,09H
        MOV     DX,OFFSET EXPLAIN_OUTPUT_SCAN
        INT     21H

        PUSH    BX 
        CALL    OUTPUT_NUM_DEC

        CMP     BX,1
        JNZ     MAIN_LOOP_1

        MOV     AX,4C00H
        INT     21H


    
MAIN    ENDP

OUTPUT_NUM_DEC  PROC    NEAR ;[SP+2]:num

        PUSH    BP
        MOV     BP,SP
        PUSH    SI
        PUSH    DI
        PUSH    AX
        PUSH    BX
        PUSH    CX
        PUSH    DX

        MOV     AX,[BP+4]
        MOV     SI,10
        MOV     DI,0
        MOV     BP,OFFSET ASCII

    HEX2DEC_LOOP2:

        MOV     DX,0

        MOV     CX,10 
        DIV     CX

        MOV     DS:[BP+DI],DL 
        INC     DI
        CMP     AX,0
        JNE     HEX2DEC_LOOP2

        MOV     CX,DI

    HEX2DEX_LOOP3:

        DEC     DI  
        MOV     DL,DS:[BP+DI]

        PUSH    DX
        CALL    OUTPUT_NUM

        LOOP    HEX2DEX_LOOP3

        POP     DX
        POP     CX
        POP     BX
        POP     AX
        POP     DI
        POP     SI
        POP     BP

        RET     2
        

OUTPUT_NUM_DEC  ENDP

OUTPUT_NUM  PROC    NEAR    ;[SP+2]:num(byte)
        PUSH    BP
        MOV     BP,SP
        PUSH    AX
        PUSH    DX

        MOV     DL,[BP+4]
        CMP     DL,9
        JA      OUTPUT_NUM_ALPHA 

        ADD     DL,'0'
        JMP     OUTPUT_NUM_OUTPUT
    OUTPUT_NUM_ALPHA:
        ADD     DL,'A'-10
        
    OUTPUT_NUM_OUTPUT:
        MOV     AH,02H
        INT     21H

        POP     DX
        POP     AX
        POP     BP

        RET     2
OUTPUT_NUM  ENDP

CODE1   ENDS

END     MAIN

你可能感兴趣的:(x86基础实验-INT16读取按键)