2013-07-14 141 views
0

從此循環中獲取一個奇怪的結果。它執行的次數比應該多。它應該繼續顯示fib_2,因爲它會被重新計算。到底是怎麼回事?循環執行多次

INCLUDE Irvine32.inc 

UPPERBOUND = 47 
LOWERBOUND = 0 

.data 
userName  BYTE 33 DUP(0) ;string to be entered by user 
intro_1  BYTE "Fibonacci Numbers", 0 
intro_2  BYTE "Programmed by Marshall Todt", 0 
prompt_1  BYTE "What's your name? ", 0 
intro_3  BYTE "Hello, ", 0 
intro_4  BYTE "Enter the number of Fibonacci terms to be displayed", 0 
prompt_2  BYTE "How many Fibonacci terms do you want? ", 0 
intro_5  BYTE "Give the number as an integer in the range [1...46].", 0 
error_1  BYTE "Number of Fibonacci terms must be in the range [1-46].", 0 
fibCount  DWORD ? 
fib_1  DWORD 1 
fib_2  DWORD 1 
fib_3  DWORD ? 
goodBye_1  BYTE "Answers certified by Marshall Todt.", 0 
goodBye_2  BYTE "Good-bye, ", 0 
count  DWORD ? 

.code 
main PROC 

;Introduction 
mov  edx, OFFSET intro_1 
call WriteString 
call CrLf 
mov  edx, OFFSET intro_2 
call WriteString 
call CrLf 
call CrLf 

;getUserData 
mov  edx, OFFSET prompt_1 
call WriteString 
mov  edx, OFFSET userName 
mov  ecx, 32 
call ReadString 


;userInstructions 
mov  edx, OFFSET intro_3 
call WriteString 
mov  edx, OFFSET userName 
call WriteString 
call CrLf 
    mov  edx, OFFSET intro_4 
call WriteString 
call CrLf 
mov  edx, OFFSET intro_5 
call WriteString 
call CrLf 
reEnter: 
call CrLf 
mov  edx, OFFSET prompt_2 
call WriteString 
mov  edx, OFFSET fibCount 
mov  ecx, 32 
call ReadString 

;mov  eax, fibCount 
;cmp eax, LOWERBOUND 
;jg reEnter   ;jumps to reEnter if the number of fibonacci terms is not higher than the LOWERBOUND 
;mov  eax, fibCount 
;cmp eax, UPPERBOUND 
;jl reEnter   ;jumps to reEnter if the number of fibonacci terms is not lower than the UPPERBOUND 

mov eax, fibCount 
sub eax, 2 
mov count, eax 

;displayFibs 
mov eax, fib_1 
Call WriteDec 
Call CrLf 
L1: 
mov count, ecx 
mov eax, fib_2 
Call WriteDec 
Call CrLf 

;calculate Fibs 
mov eax, fib_1 
add eax, fib_2 
mov fib_3, eax 
mov eax, fib_2 
mov fib_1, eax 
mov eax, fib_3 
mov fib_2, eax 
mov ecx, count 
loop L1 

;farewell 
mov edx, OFFSET goodBye_1 
call WriteString 
call CrLf 
mov  edx, OFFSET goodBye_2 
call WriteString 
mov  edx, OFFSET userName 
call WriteString 
call CrLf 

exit ; exit to operating system 
main ENDP 

END main 

編輯:在代碼

+0

多少次循環執行呢?第一次進入循環時'ecx'的價值是多少?你確定'WriteDec'和'CrLf'不會修改'ecx'嗎? –

+0

我不相信他們修改ecx(但可以肯定,我添加了count變量來存儲和檢索循環之前的計數)它在一次測試中執行35次,當它執行兩次時 –

+0

似乎始終執行35次輸入號碼爲 –

回答

2

問題的其餘部分加入似乎是你不把的count值在ECX啓動循環之前。您撥打ReadString的電話顯然存儲在fibCount中的值。您從中減去2並將其存儲在count中,但您永遠不會更改ecx。在ecx中設置的唯一值是32

我想你會解決你的問題:

call CrLf 
    mov ecx, count ; <== add this line 
L1: 
    mov count, ecx 
+0

中的整數做到了這一點,並使其成爲一個無限循環 –

+0

您似乎是用一個字符串填充'fibcount',然後將其作爲整數使用。 Kip Irvine是否有'ReadInt'? –

+0

@MarshallTigerus:添加該指令不太可能會造成無限循環。你有沒有一個調試器可以用來單步執行你的代碼? –