我目前正在做一项任务,虽然前两项要求没有问题,但现在我遇到了最后一个问题。它要求我们使用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