-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.asm
64 lines (54 loc) · 1.06 KB
/
fibonacci.asm
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
;
; Source code written by Gabriel Correia
;
BITS 64
extern printf
global main, fibo
%macro FUNC_PROLOGUE 1
push rbp
mov rbp, rsp
sub rsp, %1
%endmacro
%macro FUNC_EPILOGUE 0
leave
ret
%endmacro
section .text
fibo:
; F(n) = F(n-1) + F(n-2)
; fibo (n) -> fibo (n - 1) + fibo(n - 2) while n >= 1
FUNC_PROLOGUE 16
; Create a integer in the stack
mov DWORD [rbp-4], edi
; Compare to 1
cmp DWORD [rbp-4], 1
; Jump if the number is not greter or equal to 1
jng .C1
sub DWORD [rbp-4], 1
call fibo
mov DWORD [rbp-8], eax
mov edi, DWORD [rbp-4]
sub DWORD [rbp-4], 1
call fibo
mov ebx, DWORD [rbp-8]
add eax, ebx
FUNC_EPILOGUE
.C1:
; Returns the same value
; This part of code is most like this:
; if (n <= 1)
; return n;
;
mov eax, DWORD [rbp-4]
FUNC_EPILOGUE
main:
; Caculate the fibonacci from the value 9
FUNC_PROLOGUE 16
mov edi, 9
call fibo
mov esi, eax
mov rdi, fmt
call printf
FUNC_EPILOGUE
section .data
fmt: db "%d", 0x0a