xorpd-solutions

View on GitHub

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](https://en.wikipedia.org/wiki/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`](https://c9x.me/x86/html/file_module_x86_id_327.html) 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`](https://c9x.me/x86/html/file_module_x86_id_161.html) 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]