Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Fibonacci Calculator (Input N and calculate the Nth Fibonacci Number)
- C:
- unsigned int fib(unsigned int n)
- {
- if (n <= 0)
- return 0;
- else if (n <= 2)
- return 1;
- else {
- unsigned int a,b,c;
- a = 1;
- b = 1;
- while (1) {
- c = a + b;
- if (n <= 3) return c;
- a = b;
- b = c;
- n--;
- }
- }
- }
- x86 assembly (MASM syntax):
- fib:
- mov edx, [esp+8]
- cmp edx, 0
- ja @f
- mov eax, 0
- ret
- @@:
- cmp edx, 2
- ja @f
- mov eax, 1
- ret
- @@:
- push ebx
- mov ebx, 1
- mov ecx, 1
- @@:
- lea eax, [ebx+ecx]
- cmp edx, 3
- jbe @f
- mov ebx, ecx
- mov ecx, eax
- dec edx
- jmp @b
- @@:
- pop ebx
- ret
- 32-bit x86 machine code (hex):
- 8B542408 83FA0077 06B80000 0000C383
- FA027706 B8010000 00C353BB 01000000
- C9010000 008D0419 83FA0376 078BD98B
- B84AEBF1 5BC3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement