Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ########## Recursive Function ############
- # f(t0) = ┌ x if n <= y #
- # └ g[f(t0 - z), t0] otherwise #
- ##########################################
- Main:
- addi sp, zero, 1600 # make room in stack
- ecall t0, zero, 5 # store input in t0
- add a0, t0, zero # move input to a0
- jal ra, RecLin # call function
- ecall zero, a0, 0 # print result via a0
- beq zero, zero, Done
- RecLin:
- addi sp, sp, -16 # push stack for 2 items
- sd ra, 8(sp) # store return address
- sd a0, 0(sp) # store n on stack
- addi t1, zero, [y + 1]
- bge a0, t1, Else # goto Else if a0 >= [y + 1]
- addi a0, zero, x # base case: return x
- addi sp, sp, 16 # pop stack; values not returned
- jalr zero, 0(ra)
- Else:
- addi a0, a0, -z # n -= z
- jal ra, RecLin # call RecLin(n - z)
- add t2, a0, zero # t2 = RecLin(n - z)
- ld a0, 0(sp) # restore caller's n value
- ld ra, 8(sp) # restore return address
- addi sp, sp, 16 # pop stack
- inst a0, t2, imm # a0 = RecLin(n - 5)
- inst a0, a0, t0 # a0 += n
- jalr zero, 0(ra)
- Done:
Add Comment
Please, Sign In to add comment