# MARIE and exponents

Discussion in 'Homework Help' started by Dino1996, Mar 30, 2013.

1. ### Dino1996 Thread Starter New Member

Mar 30, 2013
6
0
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:
Code ( (Unknown Language)):
1.
2. [SIZE=3][FONT=Calibri]Input     [/FONT][/SIZE]
3. [SIZE=3][FONT=Calibri]                 Store M[/FONT][/SIZE]
4. [SIZE=3][FONT=Calibri]Input     [/FONT][/SIZE]
5. [SIZE=3][FONT=Calibri]                 Store N[/FONT][/SIZE]
7. [SIZE=3][FONT=Calibri]         Store Ctr1  /Store the counter[/FONT][/SIZE]
10. [SIZE=3][FONT=Calibri]         Store Sum  /Store result in Sum[/FONT][/SIZE]
11. [SIZE=3][FONT=Calibri]         Load Ctr1   /initialize counter[/FONT][/SIZE]
12. [SIZE=3][FONT=Calibri]         Subt One   /Decrement counter[/FONT][/SIZE]
13. [SIZE=3][FONT=Calibri]         Store Ctr1  /Store counter[/FONT][/SIZE]
14. [SIZE=3][FONT=Calibri]         SkipCond 400 /discontinue loop if Ctr1=0[/FONT][/SIZE]
15. [SIZE=3][FONT=Calibri]         Jump Loop   /if AC not 0, continue loop[/FONT][/SIZE]
17. [SIZE=3][FONT=Calibri]                 Store Ctr2  [/FONT][/SIZE]
18. [SIZE=3][FONT=Calibri]                 Jump B      /off to B[/FONT][/SIZE]
20. [SIZE=3][FONT=Calibri]                 Output      /Print product[/FONT][/SIZE]
21. [SIZE=3][FONT=Calibri]         Halt        /Sum = MeN [/FONT][/SIZE]
22. [SIZE=3][FONT=Calibri]B,            Load Sum    / [/FONT][/SIZE]
23. [SIZE=3][FONT=Calibri]                 Store P     /[/FONT][/SIZE]
24. [SIZE=3][FONT=Calibri]                 Jump E       /off to E[/FONT][/SIZE]
27. [SIZE=3][FONT=Calibri]                 Store Sum    / save sum                [/FONT][/SIZE]
29. [SIZE=3][FONT=Calibri]                 Subt One    /subtract 1 from counter[/FONT][/SIZE]
30. [SIZE=3][FONT=Calibri]                 Store Ctr1  /store value of counter[/FONT][/SIZE]
31. [SIZE=3][FONT=Calibri]                 SkipCond 400 /skip next line if current value of ctr1 = 0[/FONT][/SIZE]
32. [SIZE=3][FONT=Calibri]                 Jump Loop1       /off to loop1[/FONT][/SIZE]
34. [SIZE=3][FONT=Calibri]                 Subt One            /subtract 1[/FONT][/SIZE]
35. [SIZE=3][FONT=Calibri]                 Store Ctr2           /store counter 2[/FONT][/SIZE]
37. [SIZE=3][FONT=Calibri]F,            Store Ctr1    /saved value for counter 1[/FONT][/SIZE]
39. [SIZE=3][FONT=Calibri]                 SkipCond 400   /checking for valve 0[/FONT][/SIZE]
40. [SIZE=3][FONT=Calibri]                 Jump Loop1       /if not 0 off to loop1[/FONT][/SIZE]
41. [SIZE=3][FONT=Calibri]                 Jump C                                /now we are done[/FONT][/SIZE]
43. [SIZE=3][FONT=Calibri]                 Subt One[/FONT][/SIZE]
44. [SIZE=3][FONT=Calibri]                 Store Ctr1[/FONT][/SIZE]
45. [SIZE=3][FONT=Calibri]                 Jump B[/FONT][/SIZE]
46. [SIZE=3][FONT=Calibri]E,            Load M       /Starting new counters[/FONT][/SIZE]
47. [SIZE=3][FONT=Calibri]                 Store Ctr2   /Counter 2 [/FONT][/SIZE]
49. [SIZE=3][FONT=Calibri]                 Subt One     /I have the first value for P so counter is starting now[/FONT][/SIZE]
50. [SIZE=3][FONT=Calibri]                 Jump F       /off to F[/FONT][/SIZE]
51.
52. [SIZE=3][FONT=Calibri]  Ctr1,  Dec 0 /Used for first couter to find [/FONT][/SIZE]
53. [SIZE=3][FONT=Calibri]  Ctr2,  Dec 0 /going to use the N input as counter 2[/FONT][/SIZE]
54. [SIZE=3][FONT=Calibri]  M,     Dec 0 /initial value of M[/FONT][/SIZE]
55. [SIZE=3][FONT=Calibri]  P,          Dec 0 /init value of P[/FONT][/SIZE]
56. [SIZE=3][FONT=Calibri]  Sum,   Dec 0 /initial value of Sum[/FONT][/SIZE]
57. [SIZE=3][FONT=Calibri]  One,   Dec 1 /constant value 1[/FONT][/SIZE]
58. [SIZE=3][FONT=Calibri]  Two,   Dec 2 /constant Value 2[/FONT][/SIZE]
59. [SIZE=3][FONT=Calibri]  N,         Dec 0 /Initial value of M
60. [SIZE=3][/SIZE][/FONT][/SIZE]

Last edited by a moderator: Mar 31, 2013
2. ### WBahn Moderator

Mar 31, 2012
23,201
6,993
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.

3. ### Dino1996 Thread Starter New Member

Mar 30, 2013
6
0
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

4. ### WBahn Moderator

Mar 31, 2012
23,201
6,993
So describe the method you are trying to use.

5. ### Dino1996 Thread Starter New Member

Mar 30, 2013
6
0
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.

6. ### WBahn Moderator

Mar 31, 2012
23,201
6,993
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.

7. ### takao21203 AAC Fanatic!

Apr 28, 2012
3,783
502
ASSUMBLA.
And a decent Spaghetti meal.

Javascript can do such things.
Or do you want to learn assembler?

8. ### Dino1996 Thread Starter New Member

Mar 30, 2013
6
0
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!

9. ### absf Senior Member

Dec 29, 2010
1,873
512

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

10. ### WBahn Moderator

Mar 31, 2012
23,201
6,993
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?

11. ### Dino1996 Thread Starter New Member

Mar 30, 2013
6
0
Sorry, you are correct, any positive whole number. Im new at this

12. ### Dino1996 Thread Starter New Member

Mar 30, 2013
6
0
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.

13. ### WBahn Moderator

Mar 31, 2012
23,201
6,993
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.