0

我创建了这个程序,它接受两个输入并将它们打印出来(很简单,是的,但它是为了练习)。它编译并运行良好,但它没有达到我的预期。这是我的代码:

.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."为什么会这样?

任何帮助,将不胜感激。

问候,

程序

4

1 回答 1

3

显然,hlt 将停止执行。它只能用于等待下一个硬件中断,并且只能由操作系统使用。

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
于 2012-05-21T03:17:12.097 回答