为了在MASM中练习组装,我创建了一个小程序,该程序应该执行以下操作:
- 在屏幕上打印"键入a:">
- 从输入缓冲区读取一个字符,然后对其进行刷新
- 如果字符是"a",则脱离循环并结束程序,否则,从第一步开始重复
我的代码如下:
.386
.model flat,stdcall
include masm32includekernel32.inc ; Defines Symbols To Be Used for the kernel32 library
includelib masm32libkernel32.lib
STD_OUTPUT_HANDLE equ -11
STD_INPUT_HANDLE equ -10
.code
entryPt proc
local inHandle:DWORD
local outHandle:DWORD
local noOfCharsWritten:DWORD
; Get Standard Output Handle
push STD_OUTPUT_HANDLE
call GetStdHandle
mov outHandle,eax
; Get Standard Input Handle
push STD_INPUT_HANDLE
call GetStdHandle
mov inHandle,eax
.while (eax == eax) ; while (true)
; Print "Type a: "
push 0
lea eax,noOfCharsWritten
push eax
push sizeof txt
push offset txt
push outHandle
call WriteConsoleA
; Poll for a byte
call getChar
.if (al == "a") ; if the byte was "a"...
.break ; ...then end the loop
.endif
.endw
; Leave with exit code 0
push 0
call ExitProcess
entryPt endp
getChar proc
local inHandle:DWORD
local noOfCharsRead:DWORD
local resBt:BYTE
; Get the Standard Input Handle
push STD_INPUT_HANDLE
call GetStdHandle
mov inHandle,eax
; Read one char from the console, put the result in resBt and the number of chars read in noOfCharsRead
push 0
lea eax,noOfCharsRead
push eax
push 1
lea eax,resBt
push eax
push inHandle
call ReadConsoleA
; Flush Console Input Buffer
push inHandle
call FlushConsoleInputBuffer
; Return the result in the accumulator
movzx eax,resBt
ret
getChar endp
.data
txt db "Type a: "
end entryPt
当键入"a"时,程序将退出,就像它应该退出一样。但是,如果我键入任何不是"a"(例如"s"(的内容,而不是再次查询"Typea:",只查询一次,它将在查询另一个字节之前写入"Typea:Typea:Typea:"。写多个非a字符会导致更多的"键入a:"s。
我怀疑这是因为ReadConsole
正在读取旧的输入,因此提前终止了函数,但FlushConsoleInputBuffer
不应该清除旧的输入吗?
ReadConsole
从控制台输入缓冲区读取所有可用字符,并将它们存储在不受FlushConsoleInputBuffer
影响的单独缓冲区中。您无法直接访问该缓冲区,也无法获取有关该缓冲区的信息。因此,您必须使用ReadConsole
读取该缓冲区直到行尾。默认情况下,行的末尾标记有两个字节CR(0x0D(和LF(0x0A(。由于只读取一个字节,所以缓冲区中至少还剩下LF。
用ReadConsole
循环替换FlushConsoleInputBuffer
以清空缓冲区,直到读取LF:
.model flat,stdcall
include masm32includekernel32.inc ; Defines Symbols To Be Used for the kernel32 library
includelib masm32libkernel32.lib
STD_OUTPUT_HANDLE equ -11
STD_INPUT_HANDLE equ -10
.code
entryPt proc
local inHandle:DWORD
local outHandle:DWORD
local noOfCharsWritten:DWORD
; Get Standard Output Handle
push STD_OUTPUT_HANDLE
call GetStdHandle
mov outHandle,eax
; Get Standard Input Handle
push STD_INPUT_HANDLE
call GetStdHandle
mov inHandle,eax
.while (eax == eax) ; while (true)
; Print "Type a: "
push 0
lea eax,noOfCharsWritten
push eax
push sizeof txt
push offset txt
push outHandle
call WriteConsoleA
; Poll for a byte
call getChar
.if (al == "a") ; if the byte was "a"...
.break ; ...then end the loop
.endif
.endw
; Leave with exit code 0
push 0
call ExitProcess
entryPt endp
getChar proc
local inHandle:DWORD
local noOfCharsRead:DWORD
local resBt:BYTE, dummy:BYTE
; Get the Standard Input Handle
push STD_INPUT_HANDLE
call GetStdHandle
mov inHandle,eax
; Read one char from the console, put the result in resBt and the number of chars read in noOfCharsRead
push 0
lea eax,noOfCharsRead
push eax
push 1
lea eax,resBt
push eax
push inHandle
call ReadConsoleA
; Flush
mov al, resBt
mov dummy, al
FlushLoop:
cmp dummy, 0Ah
je EndOfFlush
invoke ReadConsoleA, inHandle, ADDR dummy, 1, ADDR noOfCharsRead, 0
jmp FlushLoop
EndOfFlush:
; Return the result in the accumulator
movzx eax,resBt
ret
getChar endp
.data
txt db "Type a: "
end entryPt