Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; *************************************************************************
- ; 32-bit Windows Console Fibonacci number calculator
- ; Very simple Fibonacci calculator implemented in MASM.
- ; Created by Ismael Vazquez (https://www.iamismael.com)
- ; *************************************************************************
- .386 ; Enable 80386+ instruction set
- .model flat, stdcall ; Flat, 32-bit memory model (not used in 64-bit)
- option casemap: none ; Case insensitive syntax
- ; *************************************************************************
- ; MASM32 proto types for Win32 functions and structures
- ; *************************************************************************
- include c:\masm32\include\kernel32.inc
- include c:\masm32\include\masm32.inc
- ; *************************************************************************
- ; MASM32 object libraries
- ; *************************************************************************
- includelib c:\masm32\lib\kernel32.lib
- includelib c:\masm32\lib\masm32.lib
- ; *************************************************************************
- ; Our executable assembly code starts here in the .code section
- ; *************************************************************************
- .code
- start:
- Main Proc
- push 09h ; FIB(9) - Change this to get different results
- call Fib
- add esp, 04h ; remove argument off the stack
- call ExitProcess
- ret
- Main EndP
- Fib Proc
- ; function prologue
- push ebp
- mov ebp, esp
- push ebx ; callee save register
- ; initialize our variables
- xor edx, edx ; f (n - 1)
- xor eax, eax ; return value
- mov ecx, 02h ; iterator
- mov ebx, 01h ; f (n - 2)
- ; If argument equals 0, return 0
- cmp dword ptr [ebp + 08h], 0h
- jne fib_loop
- jmp fib_exit
- fib_loop:
- ; while ECX(iterator) <= ARG
- cmp ecx, dword ptr [ebp + 08h]
- ja fib_exit
- ; f(n-1) += f (n-2)
- add edx, ebx
- ; store result in eax
- mov eax, edx
- ; f (n - 1) = f (n - 2)
- mov edx, ebx
- ; f (n - 2) = next number
- mov ebx, eax
- inc ecx
- jmp fib_loop
- fib_exit:
- ; restore caller save register
- pop ebx
- ; function epilogue
- mov esp, ebp
- pop ebp
- ret
- Fib EndP
- end start
Advertisement