程序将接受来自用户的两个数字,并显示这两个数字的总和,产品和功率(a ^ b)。 这是捕获,但是......
程序必须: 使用AddNumbers函数 在MultiplyNumbers函数中使用AddNumbers函数 在CalculatePower函数中使用MultiplyNumbers函数
我无法通过实现乘法加法功能找出如何找到两个数的乘积。我无法理解它背后的逻辑。我显然做错了什么,但我不确定是什么。 (请忽略代码的低效率,我只想知道在找到两者的产品时我做错了什么)
我的代码到目前为止..
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
.code
main PROC
call GetInteger
call Crlf
mov eax, ebx
call AddNumbers
mov edx, OFFSET str2
call WriteString
call WriteInt
call Crlf
mov eax, 0
mov ecx, edi
call MultiplyNumber
mov eax, edx
mov edx, OFFSET str3
call WriteString
call WriteInt
call Crlf
call CalculatePower
mov eax, esi
mov edx, OFFSET str4
call WriteString
call WriteInt
call Crlf
exit
main ENDP
GetInteger PROC
mov edx, OFFSET str1
call WriteString
call ReadInt
mov ebx, eax
call WriteString
call ReadInt
mov edi, eax
ret
GetInteger ENDP
CalculatePower PROC USES edi ebx
mov ecx, edi
mov esi, 0
L2:
call MultiplyNumber
add esi, edx
loop L2
ret
CalculatePower ENDP
MultiplyNumber PROC USES ebx edi ecx
mov edx, 0
L1:
mov edi, ebx
mov eax, 0
call AddNumbers
add edx, eax
loop L1
ret
MultiplyNumber ENDP
AddNumbers PROC
add eax, edi
ret
AddNumbers ENDP
END main
答案 0 :(得分:0)
试试这个。您的代码分析很复杂,因为您在不同的过程中有不同的循环而无需再次初始化ecx等。
OPTION CASEMAP:NONE
INCLUDE Irvine32.inc
ExitProcess proto, dwExitCode:dword
.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
tmp DWORD 0
.code
main PROC
mov edx, OFFSET str1 ;// Input
call WriteString
call ReadInt
mov [num1], eax
mov edx, OFFSET str1
call WriteString
call ReadInt
mov [num2], eax
call doSum ;// Calculations
call doMul
call doPow
mov edx, OFFSET str2 ;// Output
call WriteString
mov eax, [sum]
call WriteInt
call Crlf
mov edx, OFFSET str3
call WriteString
mov eax, [prod]
call WriteInt
call Crlf
mov edx, OFFSET str4
call WriteString
mov eax, [pow]
call WriteInt
call Crlf
invoke ExitProcess, 0
main ENDP
doSum PROC ;// Sum
mov eax, [num1]
add eax, [num2]
mov [sum], eax
ret
doSum ENDP
doMul PROC ;// Multiply: Add num1 x num2 times
xor eax, eax
mov ecx, [num2]
ADD_LOOP:
add eax, [num1]
loop ADD_LOOP
mov [prod], eax
ret
doMul ENDP
doPow PROC ;// Power: Add num1 x num2 times and
mov eax, [num1] ;// add result x num2 times till ebx=0
mov [tmp], eax
mov ebx, [num2]
dec ebx
POW_LOOP:
xor eax, eax
mov ecx, [num1]
ADDPOW_LOOP:
add eax, [tmp]
loop ADDPOW_LOOP
mov [tmp], eax
dec ebx
jnz POW_LOOP
mov [pow], eax
ret
doPow ENDP
END main
通过乘法和幂处理调用添加proc的修改版本:
OPTION CASEMAP:NONE
INCLUDE Irvine32.inc
ExitProcess proto, dwExitCode:dword
.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
tmp DWORD 0
.code
main PROC
mov edx, OFFSET str1 ;// Input
call WriteString
call ReadInt
mov [num1], eax
mov edx, OFFSET str1
call WriteString
call ReadInt
mov [num2], eax
mov eax, [num1] ;// Calculations
mov ebx, [num2]
mov ecx, 1
call doSum
mov [sum], eax
call doMul
call doPow
mov edx, OFFSET str2 ;// Output
call WriteString
mov eax, [sum]
call WriteInt
call Crlf
mov edx, OFFSET str3
call WriteString
mov eax, [prod]
call WriteInt
call Crlf
mov edx, OFFSET str4
call WriteString
mov eax, [pow]
call WriteInt
call Crlf
invoke ExitProcess, 0
main ENDP
doSum PROC ;// Sum: (add ebx to eax) * ecx times
SUM_LOOP: ;// when ecx = 1 => simple addition a+b
add eax, ebx
loop SUM_LOOP
ret
doSum ENDP
doMul PROC ;// Multiply: Add num1 x num2 times
xor eax, eax
mov ebx, [num1]
mov ecx, [num2]
call doSum ;// call Sum proc
mov [prod], eax
ret
doMul ENDP
doPow PROC ;// Power: Add num1 x num2 times and
mov eax, [num1] ;// add result x num2 times till end
mov [tmp], eax
mov esi, [num2]
dec esi
POW_LOOP:
xor eax, eax
mov ebx, [tmp]
mov ecx, [num1]
call doSum ;// Call Sum proc
mov [tmp], eax
dec esi
jnz POW_LOOP
mov [pow], eax
ret
doPow ENDP
END main
答案 1 :(得分:0)
这一点很容易相信我。我只是不知道它是如何起作用的。呵呵。
%include "asm_io.inc"
segment .data
prompt db "Enter first num ",0
prompt1 db "Enter second num ",0
prompt2 db "Equals ",0
segment .bss
input1 resd 1
input2 resd 1
segment .text
global _asm_main
_asm_main:
enter 0,0
pusha
mov eax, prompt
call print_string
call read_int
mov [input1], eax
mov eax, prompt1
call print_string
call read_int
mov [input2], eax
mov eax, prompt2
call print_string
mov eax, [input1]
mov ebx, [input2]
mov ebx, eax
mul ebx
call print_int
popa
mov eax,0
mov ebx,0
leave
ret