Skip to content

Instantly share code, notes, and snippets.

@scaryghost
Created April 1, 2014 03:41
Show Gist options
  • Save scaryghost/9907301 to your computer and use it in GitHub Desktop.
Save scaryghost/9907301 to your computer and use it in GitHub Desktop.
fibanocci in x64
# compile: gcc -o fib_x64 fib_x64.s -m64
# run: ./fib <number>
.global main
.data
formatstr: .asciz "fib(%d)= %d\n"
main:
push %r12
movq 8(%rsi),%rdi
call atoi
movq %rax,%rdi
movq %rax,%r12
call fib
movq $formatstr,%rdi
movq %r12,%rsi
movq %rax,%rdx
movq $0,%rax
call printf
call exit
fib:
push %rbp
movq %rsp, %rbp
cmpq $1,%rdi
je fib_base
cmpq $0,%rdi
je fib_base
decq %rdi
push %rdi
call fib
pop %rdi
subq $8, %rsp
movq %rax, (%rsp)
decq %rdi
push %rdi
call fib
pop %rdi
movq (%rsp), %rcx
addq $8, %rsp
addq %rcx,%rax
leave
ret
fib_base:
movq $1,%rax
leave
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment