ismaelvazquezjr

Assembonacci

Oct 20th, 2019
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; *************************************************************************
  2. ; 32-bit Windows Console Fibonacci number calculator
  3. ; Very simple Fibonacci calculator implemented in MASM.
  4. ; Created by Ismael Vazquez (https://www.iamismael.com)
  5. ; *************************************************************************
  6.                                    
  7. .386                    ; Enable 80386+ instruction set
  8. .model flat, stdcall    ; Flat, 32-bit memory model (not used in 64-bit)
  9. option casemap: none    ; Case insensitive syntax
  10.  
  11. ; *************************************************************************
  12. ; MASM32 proto types for Win32 functions and structures
  13. ; *************************************************************************  
  14. include c:\masm32\include\kernel32.inc
  15. include c:\masm32\include\masm32.inc
  16.          
  17. ; *************************************************************************
  18. ; MASM32 object libraries
  19. ; *************************************************************************  
  20. includelib c:\masm32\lib\kernel32.lib
  21. includelib c:\masm32\lib\masm32.lib
  22.  
  23. ; *************************************************************************
  24. ; Our executable assembly code starts here in the .code section
  25. ; *************************************************************************
  26. .code
  27.  
  28. start:
  29.     Main Proc
  30.         push 09h ; FIB(9) - Change this to get different results
  31.         call Fib
  32.         add esp, 04h ; remove argument off the stack
  33.        
  34.         call ExitProcess
  35.         ret
  36.     Main EndP
  37.     Fib Proc
  38.         ; function prologue
  39.         push ebp
  40.         mov ebp, esp
  41.        
  42.         push ebx   ; callee save register
  43.        
  44.         ; initialize our variables
  45.         xor edx, edx ; f (n - 1)
  46.         xor eax, eax ; return value
  47.         mov ecx, 02h ; iterator
  48.         mov ebx, 01h ; f (n - 2)
  49.        
  50.         ; If argument equals 0, return 0
  51.         cmp dword ptr [ebp + 08h], 0h
  52.         jne fib_loop
  53.         jmp fib_exit
  54.        
  55.     fib_loop:
  56.        
  57.         ; while ECX(iterator) <= ARG
  58.         cmp ecx, dword ptr [ebp + 08h]
  59.         ja fib_exit
  60.        
  61.         ; f(n-1) += f (n-2)
  62.         add edx, ebx
  63.        
  64.         ; store result in eax
  65.         mov eax, edx
  66.        
  67.         ; f (n - 1) = f (n - 2)
  68.         mov edx, ebx
  69.         ; f (n - 2) = next number
  70.         mov ebx, eax
  71.        
  72.         inc ecx
  73.         jmp fib_loop
  74.        
  75.     fib_exit:
  76.         ; restore caller save register
  77.         pop ebx
  78.         ; function epilogue
  79.         mov esp, ebp
  80.         pop ebp
  81.         ret
  82.     Fib EndP
  83. end start
Advertisement