我写了这个程序,将两个数字相乘在一起,但现在我需要使它工作的负数,任何人都有任何建议?
org 100
Input a /first factor
Store a /store first factor
Input b /second factor
Store b /store second factor
Load a /load first factor
Skipcond 800 /checks if number is greater than zero
Jump End /skips to the end if zero
load b /load second factor
Skipcond 800 /checks if number is greater than zero
Jump End /skips to the end if zero
Loop, Load b /reloads second factor
Subt One /subtracts one from the second factor
Store b /store the new value of the second factor
Load productAB /load the product of the two number; initially zero
Add a /add first factor to the product
Store productAB /store new value to product
Load b/ load second factor again
Skipcond 400 /end loop if b is equal to 0
Jump Loop /repeats the loop
Load productAB /load the value of productAB; no longer 0
End, output /print out results
Halt /end of program
a, Dec 0
b, Dec 0
productAB, Dec 0 /product of the first two numbers
One, Dec 1
2条答案
按热度按时间vawmfj5a1#
使用伪代码
dced5bon2#
下面是适用于所有整数的乘法算法的优化版本的伪代码:
通过实现交换功能,代码在将一个大数字和一个小数字相乘时需要更少的迭代。例如,当乘以
1000*2
时,修改后的算法只做1000+1000
而不是2+2+...+2
。下面是相应的MARIE.js代码: