MARIE and exponents

Thread Starter

Dino1996

Joined Mar 30, 2013
6
MARIE and exponents
Im trying to write a program in MARIE assembly where you can put in any 2 numbers (X to the Y power) and it will find your answer. I am completely stuck and need some help. This is what I have so far:
Rich (BB code):
Input     
                 Store M
Input     
                 Store N
         Load M     /load 1st value as counter
         Store Ctr1  /Store the counter
Loop,    Load Sum   /Load sum
         Add M      /Add M to Sum
         Store Sum  /Store result in Sum
         Load Ctr1   /initialize counter
         Subt One   /Decrement counter
         Store Ctr1  /Store counter
         SkipCond 400 /discontinue loop if Ctr1=0
         Jump Loop   /if AC not 0, continue loop
Endloop, Load N      /loading to counter
                 Store Ctr2  
                 Jump B      /off to B
C,            Load Sum
                 Output      /Print product
         Halt        /Sum = MeN 
B,            Load Sum    / 
                 Store P     /
                 Jump E       /off to E
Loop1,  Load P      / load P
                 Add Sum     /add sum
                 Store Sum    / save sum                
                 Load Ctr1   /load counter 1
                 Subt One    /subtract 1 from counter
                 Store Ctr1  /store value of counter
                 SkipCond 400 /skip next line if current value of ctr1 = 0
                 Jump Loop1       /off to loop1
EndLoop1, Load Ctr2       /load counter2 
                 Subt One            /subtract 1
                 Store Ctr2           /store counter 2
                 Load M            /load M to get ready to use it as the counter again
F,            Store Ctr1    /saved value for counter 1
                 Load Ctr2            / load counter 2
                 SkipCond 400   /checking for valve 0
                 Jump Loop1       /if not 0 off to loop1
                 Jump C                                /now we are done
D,            Load M
                 Subt One
                 Store Ctr1
                 Jump B
E,            Load M       /Starting new counters
                 Store Ctr2   /Counter 2 
                 Load M       /Loading M for counter 1
                 Subt One     /I have the first value for P so counter is starting now
                 Jump F       /off to F

  Ctr1,  Dec 0 /Used for first couter to find 
  Ctr2,  Dec 0 /going to use the N input as counter 2
  M,     Dec 0 /initial value of M
  P,          Dec 0 /init value of P
  Sum,   Dec 0 /initial value of Sum
  One,   Dec 1 /constant value 1
  Two,   Dec 2 /constant Value 2
  N,         Dec 0 /Initial value of M

 
Last edited by a moderator:

WBahn

Joined Mar 31, 2012
30,086
You don't really expect someone to walk through your code line by line tryng to find where it is going wrong when you don't give any indication of what it is supposed to be doing or what it is actually doing or what steps you have tried to debug it, do you?

At the very least, provide a flow chart or a high-level language equivalent of your algorithm.
 

Thread Starter

Dino1996

Joined Mar 30, 2013
6
Sorry, you are correct. My issue is I need find a way to find the sum of x to the power of y using only addition I thought I had it but I just cant seem to figure out a way
 

Thread Starter

Dino1996

Joined Mar 30, 2013
6
What I was trying to do was use addition, two counters and some loops but my program only worked for 5 to the 4th power.
 

WBahn

Joined Mar 31, 2012
30,086
Again, we are NOT mind readers. Tell us what your algorithm is without making use reverse engineer it from your code, what may well have bugs in it.
 

Thread Starter

Dino1996

Joined Mar 30, 2013
6
I need to learn it for a class. I figured it out, if you have x to the power of y, you first loop x plus itself x times make that new number, z, and then add that to itself x times. Run that loop y-1 times. I just needed to take care of 0 and 1 separately and all was good!:)
 

absf

Joined Dec 29, 2010
1,968
I dont quite get your description on how your program works...

Now let us use "3 to the power 4" ie 3x3x3x3=81 as an example. Can you walk me through your program and show us how it get 81 as the answer?

Allen
 

WBahn

Joined Mar 31, 2012
30,086
I need to learn it for a class. I figured it out, if you have x to the power of y, you first loop x plus itself x times make that new number, z, and then add that to itself x times. Run that loop y-1 times. I just needed to take care of 0 and 1 separately and all was good!:)
At best, you are describing an algorithm that might work if x and y are both positive integers. Your initial post says that the user can enter ANY to numbers.

What if they enter 3.2 and -4.5?

Once again, we are NOT mind readers. Would you PLEASE clearly state what you are trying to do, including any constraints on the input values, and the algorithm you are trying to implement?
 

Thread Starter

Dino1996

Joined Mar 30, 2013
6
sure 3 to the power of 4: 3+3+3 = 9 , (you do this 3 times, 1 - 4 always subtract 1 from the exponent) 9+9+9 = 27, 27+27+27 = 81 I also had to put in filters for the ones and zeroes.
 

WBahn

Joined Mar 31, 2012
30,086
While your algorithm is valid, consider how many operations would need to be performed in order to calculate 45000^2, which is not even large enough to overflow a 32-bit signed integer.

You might want to consider writing a routine that can multiply x and y using only addition and then use that routine when performing your exponentiation.

Another approach which can squeeze even more out of the time, is to recognize that

z=x^y

is trivial when y is an integer power of two and that any binary number is the weighted addition of integer powers of two.
 
Top