-1

This program uses a loop that multiplies two positive numbers by using repeated addition. I need help in optimizing the program in a way such that it would loop as least times as possible. For example, the program would only loop 3 times to calculate 3*6. That is, 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
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

1 Answers1

1

Given the 3 * 6 example, this multiplication will loop either 6 times or 3 times depending on the order of the parameters.  So, if you ensure to enter the loop with the smaller parameter as the loop counter and the larger as the addend, it will loop fewer times.

For example, check if one is larger and swap if necessary, before the loop.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Erik Eidt
  • 23,049
  • 2
  • 29
  • 53