Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .section .text
- .global main
- .intel_syntax noprefix
- # takes value in rdi and returns in rax
- fib:
- cmp rdi, 1
- jl .br1 # if (n < 1)
- je .br2 # else if (n == 1)
- jmp .normal # else
- .br1:
- mov rax, 0 # return 0;
- ret
- .br2:
- mov rax, 1 # return 1;
- ret
- .normal:
- # int x = fib(n - 1);
- push rdi
- sub rdi, 1
- call fib
- # int y = fib(n - 2);
- pop rdi
- sub rdi, 2
- push rax
- call fib
- # int result = x + y;
- mov rbx, rax
- pop rax
- add rbx, rax
- # return result;
- mov rax, rbx
- ret
- main:
- push rbp
- mov rbp, rsp
- sub rsp, 16
- # printf("Enter n: ");
- lea rdi, qword ptr [rip+.fmt_prompt]
- xor rax, rax
- call printf@plt
- # scanf("%d", &n);
- lea rdi, qword ptr [rip+.fmt_scanf]
- lea rsi, qword ptr [rip+n]
- xor rax, rax
- call scanf@plt
- # int result = fib(n);
- mov rdi, qword ptr [rip+n]
- call fib@plt
- # printf("Fibonacci of %d is %d", n, result);
- lea rdi, qword ptr [rip+.fmt_printf]
- mov rsi, qword ptr [rip+n]
- mov rdx, rax
- xor rax, rax
- call printf@plt
- add rsp, 16
- pop rbp
- # return 0;
- xor rax, rax
- ret
- .section .bss
- n:
- .zero 8
- .section .rodata
- .fmt_prompt:
- .asciz "Enter n: "
- .fmt_scanf:
- .asciz "%d"
- .fmt_printf:
- .asciz "Fibonacci of %d is %d\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement