2012-05-21 3 views
0

두 개의 입력을 받아서 인쇄하는이 프로그램을 만들었습니다 (간단하지만 예하지만 연습용입니다). 컴파일되고 잘 실행되지만 의도 한대로 작동하지 않습니다.x86 어셈블리 - masm32 : 응답 대기 관련 문제

Enter a number: Enter another number: 

그것은 수행해야합니다 :

Enter a number: 
; wait for input. 
Enter another number: 
; wait for input. 
; continue with program. 

는 왜 한 줄에 인쇄 않습니다

.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 

그것은이 출력이 표시됩니다 여기 내 코드는? 나는 프로세스를 중지하기 위해 거기에 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

은 왜 모두 한 줄에 인쇄되지 것입니다 : 또한 , 당신은 STDIN과 STDOUT에 대한 코드를 검토 혜택을 누릴 수 있습니까? 그게 무슨 일이야, 당신은 13, 10 텍스트를 추가해야합니다. 또한 실제로 400 바이트 버퍼를 원한다면 텍스트를 보관할 버퍼가 db가 아닐 것입니다. – Gunner

+0

다른 줄로 이동하면 감사합니다. 하지만 여전히 입력을 기다리지 않고 num1과 num2를 모두 표시 한 다음 입력을 기다리지 않습니다. 왜? – Progrmr

답변

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 

출력 : Output

stdcall을 사용하면 오른쪽에서 왼쪽으로 매개 변수를 밀어 지시.

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 
+0

감사 Motes. 마지막 질문 하나 : 코드를 실행했을 때, 그것은 효과가있었습니다. 그러나 프로그램이 입력을 인쇄하는 부분에 왔을 때 '이 프로그램이 응답하지 않습니다. 귀하의 예를 따르는 코드가 게시물로 편집됩니다. 너 나 좀 도와 줄 수있어? – Progrmr

+0

hlt 명령을 제거 했습니까? 또한 다중 레지스터를 사용하거나 xor를 사용하여 레지스터를 지울 필요가 없습니다. 출력과 함께 전체 작동 예제를 제공하기 위해 제공 한 코드를 업데이트했습니다. – Motes

+0

쿨, 고마워. 그건 잘된거야. 도와 주셔서 감사합니다. – Progrmr

관련 문제