Programming with FASM – Fibonacci example.

Today I review an old example from my work.
This tutorial is about the Fibonacci sequence and the goal of this tutorial about uses registers and stack, math and size of values.
Another part of this goal is how to use the console window.
The Fibonacci sequence is a sequence are equal to the addition of the second previous terms.
Example: F2 = F0+F1 = 0+1 = 1
F3 = F1+F2 = 1+1 = 2
F10 = F8+F9, etc.

The source code start with the window console:
The default PE format code is set and the start point of the program.
The section of .rdata and .idata is the area where value, messages, and import functions are set.
You can read the comments to see the parts of source code.
The size of the value is dd (integer).
The program uses EAX, EBX and ECX registers to use values for Fibonacci sequence.
Into FASM documentation is the 2.1.1 Data movement instructions part then transfers the operand to the top of stack indicated by ESP and is need to restore value by offset.
In my example the two values that are pushed onto the stack before the call to printf (based on the calling convention) are then discarded from the stack, by moving the stack pointer 4 bytes “upwards”, so that the old values of ecx and eax, that have been pushed before, can be restored.
In this case, is need to decrease the rcx to control flow instructions back.
The rcx is 0 the stops else will follow the loop.
A better approach to assembling secrets is to use this program with a debugger to see registry changes.
Is good to set the limit value to output DWORD format into the Fibonacci.

The result for number 10 using the program:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.