Skip to content

Instantly share code, notes, and snippets.

@kitayuta
Created October 4, 2016 10:31
Show Gist options
  • Save kitayuta/c22fa660162f2520e6fcff1fa045de85 to your computer and use it in GitHub Desktop.
Save kitayuta/c22fa660162f2520e6fcff1fa045de85 to your computer and use it in GitHub Desktop.
$ riscv64-unknown-elf-gcc -S riscv_fib.c
unsigned int fib(unsigned int n)
{
if (n < 2) return n;
else return fib(n - 1) + fib(n - 2);
}
.file "riscv_fib.c"
.option nopic
.text
.align 2
.globl fib
.type fib, @function
fib:
add sp,sp,-48
sd ra,40(sp)
sd s0,32(sp)
sd s1,24(sp)
add s0,sp,48
sw a0,-36(s0)
lw a4,-36(s0)
li a5,1
bgtu a4,a5,.L2
lw a5,-36(s0)
j .L3
.L2:
lw a5,-36(s0)
addw a5,a5,-1
mv a0,a5
call fib
mv s1,a0
lw a5,-36(s0)
addw a5,a5,-2
mv a0,a5
call fib
mv a5,a0
addw a5,s1,a5
.L3:
mv a0,a5
ld ra,40(sp)
ld s0,32(sp)
ld s1,24(sp)
add sp,sp,48
jr ra
.size fib, .-fib
.ident "GCC: (GNU) 6.1.0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment