assemblymarie

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


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

Solution

  • / 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.