Question: Use ripes.me programming website to fix this code and also provide a screenshot in ripes to show it's working correctly. I will attach instructions below.
Use ripes.me programming website to fix this code and also provide a screenshot in ripes to show it's working correctly. I will attach instructions below.
For computing you should define a function powa b that implements the following recursive algorithm.
powa b
if b
return
if a
return
ans a powa b
return ans
use the jal instruction to call your pow function. To pass arguments to the function you should use the a and a registers. To return the final answer, you should use the a register.
However, this function is recursive, which means the intermediate values a and a will be overwritten. To avoid this problem, use the function call stack by utilizing the stack pointer sp
Increment the stack pointer to make room for the values of a a and ra before calling pow Use the sw instruction to put the values of a a and ra into memory on the stack based on where the sp is pointing.
Call pow
When pow returns, move the value out of a into a different register recommended: any of the temporaries Then, use the lw instruction to retrieve the old values of a a and ra from the stackmemory based on where sp is pointing. Finally, return sp to its original position.
After doing all of this, you should have the value of powa b after the recursive call has been finished in a temporary register eg t
This is my code:
data
mystr: asciz "The program computes
a: word
b: word
text
globl main
main:
# Print initial string
la a mystr
li a
ecall
# Load values of a and b into registers
lw a a
lw a b
addi sp sp
sw rasp # Save the return address
sw asp # Save the value of a
sw asp # Save the value of a
# Call pow function
jal pow
# Retrieve the result and restore stack
mv a t # Move the result from t to a
lw rasp # Restore ra
lw asp # Restore a
lw asp # Restore a
addi sp sp # Move the stack pointer back to its original position
# Print the result
li a # Service number for print integer
ecall
# Exit
li a # Service number for exit
ecall
pow:
# Function implementation
# a: a b: a
addi sp sp # Allocate space for the local variable ans
sw rasp # Save return address
sw asp # Save a
li t # Initialize ans to
beqz a powexit # If b return
li t # Initialize t to
beqz a powexit # If a return
addi a a # Decrement b by
jal pow # Recursive call
lw rasp # Restore return address
lw asp # Restore a
mv a t # Move the result to a after recursive call
mul t a t # Calculate ans a powa b
powexit:
addi sp sp # Deallocate space for the local variable
jr ra
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
