25 lines
786 B
ArmAsm
25 lines
786 B
ArmAsm
# Recursive Fibonnaci
|
|
# %rdi is the number for iterations.
|
|
|
|
fib: push %rbp # push previous base pointer
|
|
movq %rsp,%rbp # setup new base pointer
|
|
|
|
if: cmpq $1, %rdi # if (a-1) > 0: goto long
|
|
jg long
|
|
|
|
quick: movq %rdi, %rax
|
|
jmp return
|
|
|
|
long: pushq %rdi # Push a onto the stack
|
|
subq $1, %rdi
|
|
call fib # Call fib(a-1)
|
|
pushq %rax # Push fib(a-1) onto the stack
|
|
subq $1, %rdi
|
|
call fib # Call fib(a-2)
|
|
popq %rdi # pop fib(a-1) into rdi
|
|
addq %rdi, %rax # rax = fib(a-1)+fib(a-2)
|
|
popq %rdi # Guarantee that a lies in rdi.
|
|
|
|
return: leave # Clean up (stack pointers?)
|
|
ret # return m (rax)
|