Skip to content

Instantly share code, notes, and snippets.

@castleberrysam
Last active March 20, 2017 22:30
Show Gist options
  • Save castleberrysam/4ee82484fd5c2710c143bc362bb94295 to your computer and use it in GitHub Desktop.
Save castleberrysam/4ee82484fd5c2710c143bc362bb94295 to your computer and use it in GitHub Desktop.
uint16_t fibonacci(uint16_t n)
{
uint16_t first = 1;
uint16_t second = 1;
while(n > 1) {
uint16_t tmp = first + second;
second = first;
first = tmp;
--n;
}
return first;
}
// r0: program counter
// r1: stack pointer
// r2-r5: function args
// r6: function return values
// mov ra, rb = lfun 0b1010, ra, rb
// and ra, c = lfun 0b1000, ra, c
fibonacci:
mov r3, 1
mov r4, 1
mov r0, fibonacci_loc0
fibonacci_loc1:
mov r5, r3
add r5, r4
mov r4, r3
mov r3, r5
add r2, -1
fibonacci_loc0:
mov r5, r2
and r5, 0xfffe
bnez r5, fibonacci_loc1
mov r6, r3
pop r0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment