How do I raise a number x to the power y in MARIE?

纵然是瞬间 提交于 2021-02-11 12:38:14

问题


I'm trying to create a MARIE program that raises a number (x) to a power (y) and provides an output. I have tried many alterations of the idea I have in mind but I am still facing errors. The code below is the closest I have gotten to solving this problem

INPUT
STORE X
STORE XNUM
INPUT
STORE Y

MULTIPLIERA,LOAD PROD
        ADD X
            STORE PROD
            LOAD XNUM
            SUBT ONE
            STORE XNUM
            SKIPCOND 400
            JUMP MULTIPLIERA
            JUMP NEWPRODUCT

NEWPRODUCT, LOAD X
            STORE XNUM
            LOAD PROD
            STORE X
            LOAD Y
            SUBT ONE
            STORE Y
            SKIPCOND 400
            JUMP MULTIPLIERA
            LOAD PROD

            OUTPUT
            HALT          

X, DEC 00
XNUM, DEC 00
Y, DEC 00
ONE, DEC 01
PROD, DEC 00

3^2 gives me a 36 and 1^3 gives me a 4

回答1:


/ Start of the main program 
Input / Enter the exponent Store y
Subt One
Store Count

Input / Enter the Base
Store x
Store y
Jns Exp

/ Ending the main program
Load Ans 
Output 
End, Halt

Exp, Hex 0
Loop2, Load Count
    Skipcond 800
    JumpI Exp
    JnS Multiplier
    Load Ans
    Store x
    Load Count
    Subt One
    Store Counter
    Jump Loop2

/ Start of the subroutine Multiplier
Multiplier, Hex 0
    Load Zero
    Store Ans
    Loop, Load x
    Skipcond 800
    JumpI Multiplier
    Load Ans
    Add y
    Store Ans
    Load x
    Subt One
    Store x
    Jump Loop

/ Declaration
x, Dec 2
y, Dec 3
Zero, Dec 0
One, Dec 1 
Ans, Dec 0 
Count, Dec 0

This Should work Fine. Let me know if you have any problems regarding this.



来源:https://stackoverflow.com/questions/58636468/how-do-i-raise-a-number-x-to-the-power-y-in-marie

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!