我目前正在做一項任務,雖然前兩項要求沒有問題,但現在我遇到了最後一個問題。它要求我們使用UDP(稱爲MultiplyNumbers
)來查找用戶(整數)提供的輸入的功能。它調用AddNumbers
,並使用循環來查找總和。 CalculatePower
然後調用MultiplyNumbers
,這樣做n次(根據輸入)我已經得到了它的小數字,如2^3或5^2,但是當我把它,如10^5,它工作咳出一個錯誤的答案。我不確定我在這裏搞砸了什麼,但任何見解都會很棒。發現用戶輸入的力量
這是我的代碼。
INCLUDE Irvine32.inc
.data
str1 BYTE "Enter a positive integer: ",0
str2 BYTE "The sum is: ",0
str3 BYTE "The product is: ",0
str4 BYTE "The power result is: ",0
num1 DWORD 0
num2 DWORD 0
sum DWORD 0
prod DWORD 0
pow DWORD 0
temp DWORD 0 // used for counting loop in MultiplyNumbers
count DWORD 0 // used for indexing loop in CalculatePower
.code
main PROC
call GetInteger; // Getting input
mov num1, eax;
call GetInteger;
mov num2, eax;
mov eax, num1; // Calculations
mov ebx, num2;
call AddNumbers;
mov edx, OFFSET str2 ;
mov eax, sum;
call displayResults
mov eax,num2;
mov temp,eax; // indexing loop counter
xor eax,eax;
call MultiplyNumbers;
mov edx, OFFSET str3;
mov eax, prod;
call displayResults
call CalculatePower;
mov edx, OFFSET str4;
call WriteString;
mov eax, pow;
call displayResults
invoke ExitProcess, 0
main ENDP
GetInteger PROC
;-----------------------------------------------------
; Displays the results of previous procedure
; Receives:
; Returns: nothing
;-----------------------------------------------------
mov edx, OFFSET str1;
call WriteString;
call ReadInt;
ret
GetInteger ENDP
AddNumbers PROC
;-----------------------------------------------------
; Displays the results of previous procedure
; Receives:
; Returns: nothing
;-----------------------------------------------------
add eax, ebx;
mov sum, eax;
ret
AddNumbers ENDP
MultiplyNumbers PROC
;-----------------------------------------------------
; Calculates the multiplication of the intgers provided by the user.
; Receives:
; Returns: nothing
;-----------------------------------------------------
mov ebx, num1;
mov ecx, temp; // setting the loop counter;
MultiLoop:
call AddNumbers;
loop MultiLoop;
mov prod, eax;
ret
MultiplyNumbers ENDP
CalculatePower PROC
;-----------------------------------------------------
; Calculates the power of the intger provided by user
; Receives:
; Returns: nothing
;-----------------------------------------------------
mov eax,num2
mov count,eax; // used to count for powerLoop
mov eax,num1;
mov temp,eax; // setting the count for the multiplication loop
xor eax,eax; // clearing eax
powerLoop:
call MultiplyNumbers;
dec count; // decreasing count for powerLoop
mov ecx,count; // moving decreased count to index for power loop;
loop PowerLoop;
mov pow, eax;
ret
CalculatePower ENDP
displayResults PROC
;-----------------------------------------------------
; Displays the results of previous procedure
; Receives:
; Returns: nothing
;-----------------------------------------------------
call WriteString ; display string
call WriteInt ; display sum
call crlf ; advance to next line
ret
DisplayResults ENDP
END main
而不是'X^Y',你似乎在計算類似'(X^2)*(Y-1)'的東西。對於2^3和5^2來說,可以達到正確的結果,但不是10^5。 – Michael