【问题标题】:Masm32 Terminate Program by Key PressMasm32 按键终止程序
【发布时间】:2014-04-15 19:19:01
【问题描述】:

我已经做了一些研究,但仍然找不到关于如何通过在终端按 F1-F12 键来终止汇编程序的有效答案。

我有一个像这样的简单程序:

.data
prompt1 BYTE "Please Input a sentence.",0Dh,0Ah,0


.code
Main Proc
mov edx,OFFSET prompt1
call WriteString

call ReadString
exit
main ENDP
main END

我一直听说这个 INT16h 来检查是否按下了某个键,但是在最简单的示例中这是如何实现的呢?提前感谢您的帮助。

【问题讨论】:

    标签: string input terminate masm32


    【解决方案1】:

    从 MASM 论坛 (posting here) 为您找到了一个很好的例子。代码不是我的,但我以前写过这样的代码。

            .XCREF
            .NOLIST
            INCLUDE \masm32\include\masm32rt.inc
            .LIST
    
    ;#########################################################################
    
            .CODE
    
    ;*************************************************************************
    
    _main   PROC
    
            print   chr$('Press Esc to Exit'),13,10
            jmp short kloop2
    
    kloop1: INVOKE  Sleep,40
    
    kloop2: call    InKyb
            jz      kloop1
    
            push    eax
            cmp     ah,0
            jz      kloop3
    
            push    2020h
            jmp short kloop4
    
    kloop3: mov     ah,20h
            push    eax
    
    kloop4: print   esp
            pop     edx
            pop     eax
            push    eax
            print   right$(uhex$(eax),4),13,10
            pop     eax
            cmp     eax,1Bh
            jnz     kloop2
    
            exit
    
    _main   ENDP
    
    ;*************************************************************************
    
    InKyb   PROC
    
    ;Polled Keyboard Input - DednDave 8, 2010
    ;
    ;This function returns a keystroke in EAX if there is one in the buffer.
    ;If the buffer is empty, the function returns immediately.
    ;
    ;If the keyboard buffer is empty, AH = 0, AL = 0, ZF = 1.
    ;If the stroke is a regular key, AH = 0, AL = key char, ZF = 0.
    ;If the stroke is an extended key, AH = extended key, AL = E0h, ZF = 0.
    ;If the stroke is a function key, AH = function key, AL = 0, ZF = 0.
    ;
    ;ECX, EDX are not preserved.
    
            call    crt__kbhit
            or      eax,eax
            jz      InKyb1
    
            call    crt__getch
            and     eax,0FFh
            jz      InKyb0
    
            cmp     al,0E0h
            jnz     InKyb1
    
    InKyb0: push    eax
            call    crt__getch
            pop     edx
            shl     eax,8
            or      eax,edx
    
    InKyb1: retn
    
    InKyb   ENDP
    
    ;#########################################################################
    
            END     _main
    

    【讨论】:

      【解决方案2】:

      看起来您正在使用 Irvine32 库。只需使用库中的ReadKey 函数:

      .data
      prompt1 BYTE "Please Input a sentence.",0Dh,0Ah,0
      
      .code
      Main:
          mov edx,OFFSET prompt1
          call WriteString
      
          call ReadString
      
          push    VK_F23
          call    WaitForKeyPress
          exit
      
      WaitForKeyPress proc VKey:byte
      ReadIt:
          mov     eax, 10
          call    Delay
          call    ReadKey
          jz      ReadIt    
          cmp     ah, VKey    
          jne     ReadIt
          ret
      WaitForKeyPress endp
      END Main
      

      对于我的键盘,我需要通过 VK_F23 才能在 F12 按键上退出。您可以尝试将VK_F12 传递给WaitForKeyPress,看看您的系统会发生什么

      【讨论】:

        猜你喜欢
        • 2017-07-13
        • 2018-12-25
        • 2021-07-06
        • 1970-01-01
        • 2020-10-10
        • 1970-01-01
        • 2020-06-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多