.text pow: sub $sp,24 sw $a1,28($sp) # Store a1 in the space provided for it by the caller sw $ra,20($sp) # Store ra in case of a function call move $v0,$a0 # Return N beq $a1,1,pow_end # IF exp==1 rem $t0,$a1,2 # Check Even or odd: beqz $t0, pow_even # pow_odd: addi $a1,-1 # exp-1 jal pow # pow(N,exp-1) mul $v0,$a0,$v0 # N*pow(N,exp-1) j pow_end # return N*pow(N,exp-1) pow_even: div $a1,$a1,2 # exp/2 jal pow # pow(N,exp/2) mul $v0,$v0,$v0 # pow(N,exp/2)^2 pow_end: lw $a1,28($sp) # Restore all saved registers and return lw $ra,20($sp) add $sp,24 jr $ra .data base_pmt: .asciiz "Enter an integer to raise to a power: " exp_pmt: .asciiz "Enter a positive integer power: " ans_pmt: .asciiz "The square is: " .text main: sub $sp,24 # Create the frame sw $ra,20($sp) sw $a0,24($sp) sw $a1,28($sp) la $a0,base_pmt # Prompt for an integer (Base) li $v0,4 syscall li $v0,5 # Read in an int and put base in $t0 syscall move $t0,$v0 la $a0,exp_pmt # Prompt for a positive integer (exponent) li $v0,4 syscall li $v0,5 # Read in an int and put base in $t0 syscall move $a1,$v0 move $a0,$t0 jal pow move $t0,$v0 la $a0,ans_pmt li $v0,4 syscall move $a0,$t0 li $v0,1 syscall lw $ra,20($sp) # Destroy the frame and return lw $a0,24($sp) lw $a1,28($sp) add $sp,24 jr $ra