Below is a beautiful algorithm in Python for computing xy recursively.
# pow(x,y) computes x raised to the y power. # y is a non-negative integer def pow(x,y): # Base case. x to the zero is 1 if y == 0: return 1 # if y is even compute x to the (y/2) # then multiply that result by itself. elif y % 2 == 0: tmp = pow(x,y/2) return tmp * tmp # if y is odd compute x times x to the (y-1) else: return x * pow(x,y-1)
Write a recursive MIPS function named pow
that takes two integer
parameters and returns xy
using the recursive algorithm above. Your main program should prompt the user
for an x and a
y and then print the result back to the console.
Make sure that your program is recursive and that it calls
itself using jal
instructions and properly stores return
addresses on the stack.
username_hw3.s
where
username
is your user name. Make sure the subject heading in the email says
CS220 HW 3
#-------------------------------------------------------- # Name: your name # Date: 3/6/2012 # Assignment: HW 3 # # This program computes the sum of the numbers below # 1000 that are divisible by 3 or 5 and leaves the # result in register $t0. #--------------------------------------------------------