-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfibonacci.c
More file actions
72 lines (62 loc) · 1.33 KB
/
fibonacci.c
File metadata and controls
72 lines (62 loc) · 1.33 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
/*
auther: Naman Tamrakar
date: 2023-07-19
level: easy
url: https://leetcode.com/problems/fibonacci-number/
question: The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
*/
#include <stdio.h>
#include <stdlib.h>
int fib_C(int n){
if (n == 0) return 0;
int a_0 = 0, a_1 = 1, i = 2;
while (i <= n) {
int t = a_1 + a_0;
a_0 = a_1;
a_1 = t;
i++;
}
return a_1;
}
__attribute__((naked))
int fib(int n){
__asm__(
// if (n == 0)
"cmpl $0, %edi;"
"je l0;"
// a_0 = 0
"movl $0, %ecx;"
// a_1 = 1
"movl $1, %edx;"
// i = 2;
"movl $1, %esi;"
"l1:"
// while (i <= n) {
"cmpl %edi, %esi;"
"je end;"
// int t = a_1 + a_0;
"movl %ecx, %eax;"
"addl %edx, %eax;"
// a_0 = a_1;
"movl %edx, %ecx;"
// a_1 = t;
"movl %eax, %edx;"
// i++;
"incl %esi;"
"jmp l1;"
"l0:"
// return 0;
"movl $0, %edx;"
"jmp end;"
"end:;"
// return a_1;
"movl %edx, %eax;"
"ret;"
);
}
int main() {
int n;
scanf("%d", &n);
printf("%d", fib(n));
return 0;
}