.text
# Here is the python program to compute pow(x,y) efficiently.
# This is the recursive version.
#
# 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 x is odd compute x times x to the (y-1)
#  else:
#    return x * pow(x,y-1)

#--------------------------------------------
# compute x raised to the y where x 
# is in $a0 and y is in $a1
# return value in $v0
#--------------------------------------------
pow:
   # save return address on stack
   addi $sp, $sp, -4  
   sw $ra, 0($sp)

   # if y == 0 then return 1.0
   bne $a1, $zero, pow_elif
   li $v0, 1
   j pow_return    

   # else check if y is even
pow_elif:
   andi $t0, $a1, 1
   bne $t0, $zero, pow_else
   srl $a1, $a1, 1
   jal pow
   mul $v0, $v0, $v0
   j pow_return

   # else y must be odd
pow_else:
   addi $a1, $a1, -1
   jal pow
   mul $v0, $a0, $v0   

pow_return:
   lw $ra, 0($sp)
   addi $sp, $sp, 4
   jr $ra