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 2^3 you should define a function pow(a, b) that implements the following recursive algorithm.
pow(a, b){
if (b ==0){
return 1}
if (a ==0){
return 0
}
ans = a * pow(a, b-1)
return ans
}
use the jal instruction to call your pow function. To pass arguments to the function you should use the a0 and a1 registers. To return the final answer, you should use the a0 register.
However, this function is recursive, which means the intermediate values a0 and a1 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 a0, a1, and ra before calling pow(). Use the sw instruction to put the values of a0, a1, and ra into memory on the stack based on where the sp is pointing.
Call pow()
When pow() returns, move the value out of a0 into a different register (recommended: any of the temporaries). Then, use the lw instruction to retrieve the old values of a0, a1, and ra from the stack(memory) based on where sp is pointing. Finally, return sp to its original position.
After doing all of this, you should have the value of pow(a, b-1) after the recursive call has been finished in a temporary register (e.g., t2).
This is my code:
.data
my_str: .asciz "The program computes 2^3...
"
a: .word 2
b: .word 3
.text
.globl _main
_main:
# Print initial string
la a0, my_str
li a7,4
ecall
# Load values of a and b into 2 registers
lw a0, a
lw a1, b
addi sp, sp,-12
sw ra,0(sp) # Save the return address
sw a1,4(sp) # Save the value of a1
sw a0,8(sp) # Save the value of a0
# Call pow() function
jal pow
# Retrieve the result and restore stack
mv a0, t0 # Move the result from t0 to a0
lw ra,0(sp) # Restore ra
lw a1,4(sp) # Restore a1
lw a0,8(sp) # Restore a0
addi sp, sp,12 # Move the stack pointer back to its original position
# Print the result
li a7,1 # Service number for print integer
ecall
# Exit
li a7,10 # Service number for exit
ecall
pow:
# Function implementation
# a: a0, b: a1
addi sp, sp,-4 # Allocate space for the local variable (ans)
sw ra,0(sp) # Save return address
sw a1,4(sp) # Save a1
li t0,1 # Initialize ans to 1
beqz a1, pow_exit # If b ==0, return 1
li t1,1 # Initialize t1 to 1
beqz a0, pow_exit # If a ==0, return 0
addi a1, a1,-1 # Decrement b by 1
jal pow # Recursive call
lw ra,0(sp) # Restore return address
lw a1,4(sp) # Restore a1
mv a0, t0 # Move the result to a0 after recursive call
mul t0, a0, t0 # Calculate ans = a * pow(a, b-1)
pow_exit:
addi sp, sp,4 # 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!