Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a recursive MIPS program that compute x to the power y, that is, xy Write

ID: 3554876 • Letter: W

Question

Write a recursive MIPS program that compute x to the power y, that is, xy Write your recursive function in Java and show how you make the call from main(). For example, in main() you should invoke power ( ) as follows: result = power(2,4); // returns 16 to main(). Then translate the recursive program of A. into MIPS. State the buttom of the stack address explicitely in your program. Your implementation should show explicitly all activities of the caller and caller that take place on the run - time stack using the call power (2, 4)

Explanation / Answer

.data
result : .space 4
.text

main:
# Function prologue -- even main has one
addiu $sp, $sp, 24 # allocate stack space -- default of 24 here
sw $fp, 0($sp) # save frame pointer of caller
sw $ra, 4($sp) # save return address
addiu $fp, $sp, 20 # setup frame pointer of main
#result = power(2,4) here 2 and 4 are passed as constant values and the returned result is stored in a variable result
#result is declared as a 4 byte, and after returning from power the returned value from $a3 is moved to result
li $a0,2
# input for power exponent in $a1 and number in $a0
li $a1,4
jal power
#the returned value from power is stored in $a3
la $t1,result
sw $a3,0($t1)
#infinite loop after calling power like while(1)
infloop:
b infloop
mainDone:
# Epilogue for main -- restore stack & frame pointers and return
lw $ra, 4($sp) # get return address from stack
lw $fp, 0($sp) # restore frame pointer for caller
addiu $sp, $sp, 24 # restore frame pointer for caller
jr $ra # return to caller

power:
# Function prologue
addiu $sp, $sp, -32 # allocate stack space
# save $a0 thru $a1
sw $a1, 12($sp)
sw $a0, 8($sp)
sw $ra, 4($sp) # save return address
sw $fp, 0($sp) # save frame pointer of caller
addiu $fp, $sp, 28 # setup frame pointer of average

li $a3,1
loop: beq $a1,$0, end
mul $a3,$a0,$a3
subi $a1,$a1,1
b loop
end: # Epilogue -- restore stack & frame pointers & return
lw $a0, 8($sp)
lw $a1,12($sp)
lw $ra, 4($sp) # get return address from stack
lw $fp, 0($sp) # restore frame pointer for caller
addiu $sp, $sp, 32 # restore frame pointer for caller
jr $ra # return to caller   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote