我创建了这个程序,它接受两个输入并将它们打印出来(简单,是的,但是它可以用于练习)。它编译,并运行良好,但它没有做我想要的。这是我的代码:
.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
.data
num1 db "Enter a number:", 0
num2 db "Enter another number:", 0
.data?
buffer1 dd 100 dup(?)
buffer2 dd 100 dup(?)
.code
start:
lea eax, num1
push eax
call StdOut
lea ebx, buffer1
push ebx
call StdIn
hlt
lea eax, num2
push eax
call StdOut
lea edx, buffer2
push edx
call StdIn
xor eax, eax
xor ebx, ebx
xor edx, edx
lea eax, buffer1
push eax
call StdOut
lea ebx, buffer2
push ebx
call StdOut
push 0
call ExitProcess
end start
显示此输出:
Enter a number: Enter another number:
应该这样做:
Enter a number:
; wait for input.
Enter another number:
; wait for input.
; continue with program.
为什么要在一行打印?我尝试将halt
放在那里以停止该过程,但Windows停止程序运行,并说the program is not responding
。
编辑:
以下是我要编辑的代码:
xor eax, eax
xor ebx, ebx
xor edx, edx
lea eax, buffer1
push eax
call StdOut
lea ebx, buffer2
push ebx
call StdOut
当我使用之前的代码运行时,它会显示"This program is not responding."
为什么会这样?
任何帮助都将不胜感激。
此致
Progrmr
答案 0 :(得分:3)
StdIn不适合您,因为您没有提供缓冲区的长度。所以StdIn失败了,下一个StdOut被执行了。
不要使用hlt,然后按缓冲区的长度,然后将地址推送到缓冲区。
.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
include \masm32\include\msvcrt.inc
include \MASM32\INCLUDE\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\msvcrt.lib
includelib \MASM32\LIB\user32.lib
atoi PROTO C strptr:DWORD
.data
num1 db "Enter a number:", 0
num2 db "Enter another number:", 0
formatStr db "%s+%s=%d", 0
.data?
buffer1 dw 100 dup(?)
buffer2 dw 100 dup(?)
buffer3 dw 100 dup(?)
.code
start:
lea eax, num1
push eax
call StdOut
mov eax,100
push eax
lea eax, buffer1
push eax
call StdIn
lea eax, num2
push eax
call StdOut
mov eax,100
push eax
lea eax, buffer2
push eax
call StdIn
lea eax, buffer1
push eax
call atoi
mov ebx,eax
lea eax, buffer2
push eax
call atoi
add eax,ebx
push eax
lea eax,buffer2
push eax
lea eax,buffer1
push eax
lea eax,formatStr
push eax
lea eax,buffer3
push eax
call wsprintf
lea eax,buffer3
push eax
call StdOut
push 0
call ExitProcess
end start
输出:
stdcall指示您从右向左推送参数。 此外,您可能会从查看StdIn和StdOut的代码中受益:
StdIn proc lpszBuffer:DWORD,bLen:DWORD
LOCAL hInput :DWORD
LOCAL bRead :DWORD
invoke GetStdHandle,STD_INPUT_HANDLE
mov hInput, eax
invoke SetConsoleMode,hInput,ENABLE_LINE_INPUT or \
ENABLE_ECHO_INPUT or \
ENABLE_PROCESSED_INPUT
invoke ReadFile,hInput,lpszBuffer,bLen,ADDR bRead,NULL
mov eax, bRead
ret
StdIn endp
StdOut proc lpszText:DWORD
LOCAL hOutPut :DWORD
LOCAL bWritten :DWORD
LOCAL sl :DWORD
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov hOutPut, eax
invoke StrLen,lpszText
mov sl, eax
invoke WriteFile,hOutPut,lpszText,sl,ADDR bWritten,NULL
mov eax, bWritten
ret
StdOut endp