Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. Fibonacci Calculator (Input N and calculate the Nth Fibonacci Number)
  2.  
  3. C:
  4. unsigned int fib(unsigned int n)
  5. {
  6. if (n <= 0)
  7. return 0;
  8. else if (n <= 2)
  9. return 1;
  10. else {
  11. unsigned int a,b,c;
  12. a = 1;
  13. b = 1;
  14. while (1) {
  15. c = a + b;
  16. if (n <= 3) return c;
  17. a = b;
  18. b = c;
  19. n--;
  20. }
  21. }
  22. }
  23.  
  24. x86 assembly (MASM syntax):
  25. fib:
  26. mov edx, [esp+8]
  27. cmp edx, 0
  28. ja @f
  29. mov eax, 0
  30. ret
  31.  
  32. @@:
  33. cmp edx, 2
  34. ja @f
  35. mov eax, 1
  36. ret
  37.  
  38. @@:
  39. push ebx
  40. mov ebx, 1
  41. mov ecx, 1
  42.  
  43. @@:
  44. lea eax, [ebx+ecx]
  45. cmp edx, 3
  46. jbe @f
  47. mov ebx, ecx
  48. mov ecx, eax
  49. dec edx
  50. jmp @b
  51.  
  52. @@:
  53. pop ebx
  54. ret
  55.  
  56. 32-bit x86 machine code (hex):
  57. 8B542408 83FA0077 06B80000 0000C383
  58. FA027706 B8010000 00C353BB 01000000
  59. C9010000 008D0419 83FA0376 078BD98B
  60. B84AEBF1 5BC3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement