Here’s how to write a Fibonacci sequence program in Assembly language from scratch for the x86-64 processor. Also includes instructions on how to install the Flat Assembler.
Installation
First, let’s install the Flat Assembler. You can download the program from here: https://flatassembler.net/download.php
Doesn’t need Windows setup/installation. Simply download the zip file and extract it to your preferred location.
Personally I extracted mine here: C:\Users\sanjib\bin\fasmw17330

Environment Variables
We need to set 2 environment variables:
- Set the INCLUDE variable to the INCLUDE folder in your fasm folder (where you extracted the Flat Assembler program): this will allow you to include additional assembly files in your assembly program. Mine was: C:\Users\sanjib\bin\fasmw17330\INCLUDE
- Add to the Path variable your fasm folder (where you extracted the Flat Assembler program): this will allow you to call the fasm.exe program from anywhere in your machine. I added C:\Users\sanjib\bin\fasmw17330


Download the training.inc file
Download the training.inc file from here: https://raw.githubusercontent.com/xorpd/asm_prog_material/master/include/training.inc
We need to download this file at put it in the INCLUDES folder of your fasm directory.
We need this file as we are going to include it in our Fibonacci program. This program allows user input and output to the console.

Fibonacci Program in Assembly Language
This is how the Fibonacci program looks like in Assembly language. Please read the inline comments which explains all the lines of code. Also watch the video that shows from the beginning to end on how to install the Flat Assembler and write the program from scratch.
I mention x and y below as if they were variables. But these are just imaginary variables to explain Fibonacci algorithm.
We are using 4 registers as follows:
- eax: to store the x value
- ebx: to store the y value
- ecx: as the counter
- edx: as the tmp value during the swap
format PE console ; tells the assembler that this is a console program entry start ; marks the start of the program, see the start: label below include 'win32a.inc' ; includes the file necessary for Windows operating system include 'training.inc' ; includes the file to call read_hex and _print_eax instructions start: ; the entry-point of the program call read_hex ; reads user input and stores it in the eax register mov ecx, eax ; move content eax to ecx, will use ecx as the counter n mov eax, 0 ; now set eax to 0 (initial value of let's say x) mov ebx, 1 ; set ebx to 1 (initial value of let's say y) fib: ; the 3 lines below are the fibonacci algorithm: sets x to y, and y to x+y mov edx, eax ; store value of eax in edx (used as a tmp area) mov eax, ebx ; move content of ebx to eax (sets x) add ebx, edx ; sum ebx with value of edx (what used to be eax, sets y to x+y) jc fib_carry ; register 32-bits, not big enough to calculate over 30 hex dec ecx ; decrease counter jz exit ; jump to exit: if ecx reaches 0 jmp fib ; jump back to fib: label, creates the loop fib_carry: mov eax, -1 ; carry over occurred, mark it with value ffffffff exit: call print_eax ; print answer push 0 ; exit console program call [ExitProcess]