Question: This uses the RV 3 2 I instruction set in the RARS simulator. Please help me understand why my code does not work: . data

This uses the RV32I instruction set in the RARS simulator. Please help me understand why my code does not work:
.data
input_raw:
.word 0 # Placeholder for the input value (input_raw)
.text
.globl main
main:
# (A) Read input value (10 points)
li a7,5 # Syscall for reading an integer (a7=5)
ecall # Read the integer from the console (result in a0)
la t4, input_raw # Load address of input_raw into t4
sw a0,0(t4) # Store the input into input_raw (already in fixed-point format)
# Load the input_raw value (fixed-point format)
lw t4,0(t4) # t4 now contains the input_raw value (unsigned interpretation)
# Initialize registers
li t0,0 # t0= guess (initial guess is 0)
li t1,4194304 # t1= initial step (256*2^14 for fixed-point)
loop:
# (B) Square the guess and shift result (10 points)
add t3, t0, t1 # t3= t0+ t1(current guess)
mul t5, t3, t3 # t5=(t0+ t1)^2(lower 32 bits)
srli t5, t5,14 # t5= t5>>14(adjust for 14 fractional bits)
# (C) Compare guess with input value (20 points)
bltu t5, t4, increase_guess # If t5 input_raw, increase guess
bgeu t5, t4, decrease_guess # If t5>= input_raw, decrease guess
increase_guess:
# (D) Add to guess (20 points)
add t0, t0, t1 # t0= t0+ t1(increase guess)
j halve_step # Go to halve_step
decrease_guess:
# (D) Subtract from guess (20 points)
sub t0, t0, t1 # t0= t0- t1(decrease guess)
halve_step:
# (E) Shift step (10 points)
srli t1, t1,1 # t1= t1>>1(halve step size)
# (F) Loop until step becomes 0(20 points)
bnez t1, loop # If t1!=0, continue looping
# (G) Print final guess (10 points)
done:
mv a0, t0 # Move the final guess (output_raw) to a0
li a7,1 # Syscall for printing an integer (a7=1)
ecall # Print the final square root in raw format
# Exit the program
li a7,10 # Syscall for exit (a7=10)
ecall
This uses the RV 3 2 I instruction set in the

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!