Skip to content

Latest commit

 

History

History

0x01

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

0x01

[dec] [home] [inc]

[See this riddle on the book]

.loop:
    xadd     rax,rdx
    loop     .loop
See explanation

Fibonacci

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.

Why?

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.

[dec] [home] [inc]