Question
computer Architecture:Given this code in RISC_V using RARS answer the following questions : # Example of recursion in RISC-V with Factorial .data doneString: .asciz
computer Architecture:Given this code in RISC_V using RARS answer the following questions :
# Example of recursion in RISC-V with Factorial .data doneString: .asciz " Done."
.text
li a0, 13 # loading n into the first argument register (a0) jal fact # jumping and linking to the fact procedure li a7, 1 # result of fact is already in a0 ecall # print integer ecall (1) j exit # if we do NOT do this, the code will simply fall through to fact again
fact: addi sp, sp, -16 # adjust stack for 2 items sw ra, 8(sp) # save return address sw a0, 0(sp) # save argument addi t0, a0, -1 # subtract 1 from argument bge t0, zero, L1 # if n >= 0, go to L1 li a0, 1 # else, set return value to 1 addi sp, sp, 16 # pop 2 items from stack jr ra # and return L1: addi a0, a0, -1 # if we're here, n was >= 0, haven't reached base case jal fact # recursive call mv t0, a0 # move result of fact(n-1) to t0 lw a0, 0(sp) # restore original n to a0 lw ra, 8(sp) # and return address to ra addi sp, sp, 16 # pop 2 items from stack mul a0, a0, t0 # multiply to get result jr ra # and return exit: li a7, 4 # load ecall #4 (print string) la a0, doneString # load the address of doneString (see .data section) ecall # print doneString to the console li a7, 10 # load ecall #10 (exit) ecall # .....and exit.
For the following questions, see the code above and step through the example assembly code using the RARS simulator.
1. [5] By the end of the program, how many different return addresses have been pushed on the stack (when n = 5)?
2. [5] What is the original value of sp? What is the lowest value (when n = 5)? How many variables are stored on the stack each iteration?
3. [10] Once the base case is reached, what is the actual sequence of values (in hex) in the a0 register? When do all of the multiplications actually take place?
4. [10] As written, what is the largest value for n that the code can correctly compute? What is the limiting instruction? How would you re-write the code to increase the maximum n it can correctly compute? What would the largest possible n be in that case?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started