-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathf_fibonacci.s
More file actions
83 lines (66 loc) · 2.15 KB
/
f_fibonacci.s
File metadata and controls
83 lines (66 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# PURPOSE: This program will computer Fibonacci of n-th term
#
# NOTE:
# You can test result via https://www.omnicalculator.com/math/fibonacci
#
.data
n:
.long 11
.text
_start:
pushl n
call f_fibonacci
movl %eax, %ebx
end:
movl $1, %eax
int $0x80
# PURPOSE: Find fibonacci of n-th term
#
# INPUT:
# n - first argument
#
# OUTPUT:
# fibonacci of n-th term
#
# VARIABLES:
# -4(ebp) - holds result of first call
#
# NOTE:
# Fibonacci recursive function in highlevel language:
#
# int fib(int n)
# {
# if (n <= 2)
# return 1;
#
# return fib(n - 1) + fib(n - 2);
# }
#
.type f_fibonacci, @function
f_fibonacci:
f_fibonacci_start:
pushl %ebp
movl %esp, %ebp
f_fibonacci_body:
cmpl $2, 8(%ebp) # if n <= 2
jle return_one # jump to return_one
# setup first recursive call:
movl 8(%ebp), %eax # set first argument(n) to eax
subl $1, %eax # set eax = eax - 1
pushl %eax # push eax as argument
call f_fibonacci # call f_fibonacci
subl $4, %esp # create local storage: -4(ebp)
movl %eax, -4(%ebp) # save result of first call(eax) in local storage -4(ebp)
# setup second recursive call:
movl 8(%ebp), %eax # set first argument(n) to eax
subl $2, %eax # set eax = eax - 2
pushl %eax # push eax as argument
call f_fibonacci # call f_fibonacci
addl -4(%ebp), %eax # add result of first call( -4(ebp) ) to result of second call (eax)
jmp f_fibonacci_end # jump end of function
return_one: # this label calls only when n <= 2
movl $1, %eax # set eax = 1
f_fibonacci_end:
movl %ebp, %esp
popl %ebp
ret