.loop:
xadd rax,rdx
loop .loop
See explanation
This is probably the smallest implementation of a
Fibonacci number generator,
as long as the two registers are set to 0 and 1 (doesn't matter which one), and you
have set ecx
to the index of the desired Fibonacci number.
In other words, if you want the 7th Fibonacci number, mov ecx,7
before the loop.
The result will be in rax
.
XADD
swaps and adds
the two operands into the destination operand. In other words, the sum of rax
and rdx
is written to rax
.
Then LOOP
makes sure
that this is done until ecx
reaches 0 (it is decremented at every iteration).
When the loop ends (i,e. when ecx
is 0), the i-th Fibonacci number will be in
the destination operand rax
.