I have been given HW regarding RISC-V. The tasks is to solve the recurring equation T(n) = 2T(n/2)+n if the n or input is >=2, otherwise it returns 1. I have tried to create the solution code but it keeps giving me the (error) attempt to execute non-instruction at 0x00000000. Can someone please tell me where is my mistake and how to fix it? Thank you for your time!
Notes: I can only starts to write the code from the "Write your recursive code here...."
.globl __start
.rodata
msg_input: .string "Enter a number: "
msg_result: .string "The result is: "
newline: .string "\n"
.text
__start:
# prints msg_input
li a0, 4
la a1, msg_input
ecall
# read from standard input
li a0, 5
ecall
################################################################################
# write your recursive code here, input is in a0, store the result(integer type) to t0
jal findsum
findsum:
li t0, 2 #t0==2
blt a0, t0, L1 #if n<2 return 1
addi sp, sp, -8 #reserve stack area
sw ra, 0(sp) #save return address
sw a0, 4(sp) #save input
li t0, 2 #t0==2
div a0, a0, t0 #n=n/2
jal findsum #call findsum(n/2)
#a1=FindSum(n/2)
li t0, 2 #t0=2
mul a1, t0, a1 #a1=2*FindSum(n/2)
addi a1, a1, 2 #a1=2*FindSum(n/2)+2
j done
L1:
li a1, 1
done:
lw ra, 0(sp)
addi sp, sp, 8
jr ra
################################################################################
result:
# prints msg_result
li a0, 4
la a1, msg_result
ecall
# prints the result in t0
li a0, 1
mv a1, t0
ecall
# ends the program with status code 0
li a0, 10
ecall
User contributions licensed under CC BY-SA 3.0