#Power.asm #A program to take an int value number to a power using #recursion. #int power(int pow, int value) #{ # if(pow < 0) # System.exit(0); # else if(pow == 0) # return 1; # else # return value * pow(value, pow - 1); #} .text main: sub $sp, $sp, 24 #make space for arguments and $ra #set $a0 here (the power) li $a0, 3 #set $a1 here (the value) li $a1, 4 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, $a1 #copy contents of $a1 (the value) into $v0 bgt $a0, 1, powElse beq $a0, 1, powEnd beq $a0, $zero, powIfZero powIfZero: li $v0, 1 add $sp, $sp, 24 #revert stack pointer jr $ra powElse: sw $a0, 24($sp) #store value in caller's space sub $a0, $a0, 1 #set up new arguments jal pow lw $a0, 24($sp) mul $v0, $v0, $a1 powEnd: lw $ra, 20($sp) #restore the ra register add $sp, $sp, 24 jr $ra