Skip to content

Instantly share code, notes, and snippets.

@scaryghost
Last active August 29, 2015 13:57
Show Gist options
  • Save scaryghost/9750926 to your computer and use it in GitHub Desktop.
Save scaryghost/9750926 to your computer and use it in GitHub Desktop.
fibanocci in x86
# compile: gcc -o fib fib.s -m32
# run: ./fib <number>
.global main
.data
formatstr: .asciz "fib(%d)= %d\n"
main:
pop %ecx
movl 4(%eax),%edx
pushl %edx
call atoi
push %eax
movl %eax,%edi
call fib
pushl %eax
pushl %edi
pushl $formatstr
call printf
call exit
fib:
pushl %ebp
movl %esp, %ebp
subl $4,%esp
movl 8(%ebp),%edx
cmpl $1,%edx
je fib_base
cmpl $0,%edx
je fib_base
decl %edx
push %edx
call fib
movl %eax,-4(%ebp)
pop %edx
decl %edx
push %edx
call fib
addl -4(%ebp),%eax
leave
ret
fib_base:
movl $1,%eax
leave
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment