Skip to content

Instantly share code, notes, and snippets.

@aarroyoc
Created August 21, 2022 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarroyoc/bbfe80e783390256871fdb38c1e77abb to your computer and use it in GitHub Desktop.
Save aarroyoc/bbfe80e783390256871fdb38c1e77abb to your computer and use it in GitHub Desktop.
Fibonacci numbers in RISC-V 64
.section .data
fibs: .double 0,0,0,0,0,0,0,0,0,0,0,0
size: .word 12
space: .string " "
head: .string "The Fibonacci numbers are:\n"
printf_str: .string "%d\n"
.section .text
.global main
main:
la s0, fibs # load address of fib
la s5, size # load address of size of fibs
lw s5, 0(s5) # load size of fibs
li s2, 1 # load 1 as first number of fib
sd s2, 0(s0) # set 1 fib number as 1
sd s2, 8(s0) # set 2 fib number as 1
addi s1, s5, -2 # loop over size - 2
loop:
ld s3, 0(s0) # load N-2 number
ld s4, 8(s0) # load N-1 number
add s2, s3, s4 # calculate N
sd s2, 16(s0) #store N number
addi s0, s0, 8 # move pointer to the next number
addi s1, s1, -1 # decrement loop
bgtz s1, loop # loop
call print
j exit
print:
mv s11, ra
la s0, fibs
la s1, size
lw s1, 0(s1)
la a0, head
call printf
out:
la a0, printf_str
ld a1, 0(s0)
call printf
addi s0, s0, 8
addi s1, s1, -1
bgt s1, zero, out
jr s11
exit:
li a7, 93
li a0, 0
ecall
default:
gcc -o fib fib.s -static
clean:
rm -rf fib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment