Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .intel_syntax noprefix
- .section .data
- n: .quad 10 # define the fibonacci number that should be calculated
- .section .text
- .global _start
- _start:
- # call Fibonacci function f(n)
- push [n] # parameter: fibonacci number to calculate
- call f # call function
- add rsp, 8 # remove parameter from stack
- # print calculated Fibonacci number on stdout
- #call printnumber
- # exit process with exit code 0
- mov rax, 1
- mov rbx, 0
- int 0x80
- # f: Calculates a Fibonacci number
- # f(n) = {n, if n<=1; f(n-1)+f(n-2), else}.
- # Parameter: Integer n >= 0, passed on stack
- # Returns: Fibonacci number f(n), returned in rax
- .type f, @function
- f:
- push rbp # basepointer auf stack pushen
- mov rbp, rsp # basepointer bekommt Wert des stackpointers
- add rbp, 8 # 1.Parameter
- mov rbx, [rbp] # rbx= 1.Paramter
- cmp rbx, 0 # wenn n=0 passiert nichts, jmp zu end
- je end
- cmp rbx, 1 # wenn n=1 jmp zu baseone
- je baseone
- dec rbx # n-1
- push [rbx] # push n-1
- call f # call f(n-1)
- dec rbx # n-2
- push [rbx] # push n-2
- call f # call f(n-2)
- baseone: add rdi, 1 # rdi = rdi + 1
- end: pop rbp # basepointer wieder vom Stack entfernen
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement