#Power2.asm # #An updated version of the power function #compute power(x,y) where the result is x^y .text main: sub $sp, $sp, 24 #make space for arguments and $ra li $a0, 3 #set $a0 (x) here li $a1, 7 #set $a1 (y) here jal pow add $sp, $sp, 24 #return the stack pointer to its original value #print the result move $a0, $v0 li $v0, 1 syscall #end the program li $v0, 10 syscall pow: sub $sp, $sp, 24 # Set aside space for arguments and ra (24 bytes) sw $ra, 20($sp) #store the ra register move $v0, $a0 #copy contents of $a0 (the value of x) into $v0 bgt $a1, 1, powElse # if y > 1, jump to powElse beq $a1, 1, powEnd # if y == 1, jump to powEnd beq $a1, $zero, powIfZero # if y == 0, return 1 #end the program if we have a negative value li $v0, 10 syscall powIfZero: li $v0, 1 #copy 1 into the return register add $sp, $sp, 24 #revert stack pointer jr $ra #return powElse: sw $a1, 24($sp) #store value of y in caller's space sub $a1, $a1, 1 #set up new arguments jal pow #call pow again lw $a1, 24($sp) # revert $a1 to its original value (note this isn't really necessary) mul $v0, $v0, $a0 #multiply x and the return value, and store the result as our new #return value # continue with the code from powEnd powEnd: lw $ra, 20($sp) #restore the ra register add $sp, $sp, 24 #revert the stack pointer (pop the stack) jr $ra #return