【问题标题】:Optimize this multiply by repeated-addition code to loop the minimum number of times通过重复加法代码优化此乘法以循环最少次数
【发布时间】:2019-11-02 14:44:55
【问题描述】:

这个程序使用一个循环,通过使用将两个正数相乘 重复添加。我需要帮助来优化程序,使其循环次数尽可能少。例如, 程序只会循环 3 次来计算 3*6。即6+6+6。

ORG 100
Load Y /load second value to be used as counter
Store Ctr /Store as a counter
Loop, Load Sum /load to the sum
Add X /Add X to sum
Store Sum /Store result in Sum
Load Ctr
Subt One / Decrement counter
Store Ctr /Store counter
SkipCond 400 /if AC=0 , discontinue looping
Jump Loop /if acnot 0 , continue looping
Endloop, Load Sum
Output /Print product
Halt /sum contains the product of x and y
Ctr, Dec 0
X, Dec 0 /initial value of x
Y, Dec 0 /initial value of Y
Sum, Dec 0 /initial value of Sum
One, Dec 1 /the constant value of 1
END

【问题讨论】:

    标签: assembly optimization multiplication marie


    【解决方案1】:

    以 3 * 6 为例,此乘法将循环 6 次或 3 次,具体取决于参数的顺序。所以,如果保证进入循环时,以较小的参数作为循环计数器,较大的作为加数,循环次数会更少。

    例如,检查一个是否更大,如果需要,在循环之前交换。

    【讨论】:

      猜你喜欢
      • 2014-07-18
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      相关资源
      最近更新 更多