Skip to content

Commit

Permalink
Recursive version of riscv_fib to show off the stack viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksmckinley committed Mar 29, 2024
1 parent f5863ac commit 3514992
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions static/assembly_examples/riscv_fib_recursive.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
fib(int):
# Stack variables: | return addr | argument | n - 1 | n - 2 |

# Save return address and argument
sw ra, 0(sp)
sw a0, 4(sp)

ori t0, zero, 0
ori t1, zero, 1
beq a0, t0, RET_0
beq a0, t1, RET_1

# Recursive call for fib(n-1)
addi a0, a0, -1 # Setting arg
addi sp, sp, -16 # Making space on the stack
jal ra, fib(int)
sw a1, 8(sp) # Storing the return value
lw a0, 4(sp) # Restoring our original argument

# Recursive call for fib(n-2)
addi a0, a0, -2
addi sp, sp, -16
jal ra, fib(int)
sw a1, 12(sp)

# Adding together n-1 and n-2 and returning
lw t0, 8(sp)
lw t1, 12(sp)

add a1, t0, t1
lw ra, 0(sp)
addi sp, sp, 16
#Pseudo-Instruction: ret
jalr x0, x1, 0 #Pseudo-Instruction Translation


RET_0:
ori a1, zero, 0
addi sp, sp, 16
#Pseudo-Instruction: ret
jalr x0, x1, 0 #Pseudo-Instruction Translation
RET_1:
ori a1, zero, 1
addi sp, sp, 16
#Pseudo-Instruction: ret
jalr x0, x1, 0 #Pseudo-Instruction Translation

main:
ori a0, zero, 5
addi sp, sp, -16
jal ra, fib(int)
ori a0, zero, 1
ecall
ori a0, zero, 0
ecall

0 comments on commit 3514992

Please sign in to comment.