Fibonacci Tutorial
This is the Catnip equivalent of the Assembly Fibonacci tutorial.
It prints Fibonacci values using the std library debug_num() helper (which uses int 0x90 internally).
#include "std"
#define COUNT, 12
main();
fun main() { let a:4 = 0; let b:4 = 1; let i:4 = 0;
while (i:4 < ${COUNT}) { debug_num(b:4);
let next:4 = a:4 + b:4; a:4 = b:4; b:4 = next:4; i:4 = i:4 + 1; }
while (1) { }}How it works
Section titled “How it works”aandbtrack consecutive Fibonacci numbers.debug_num(b:4)calls the std helper that prints via debug interrupt0x90.next = a + b, then the pair advances (a = b,b = next).- The loop stops after
COUNTvalues.
Build and run (Catnip + CatLauncher)
Section titled “Build and run (Catnip + CatLauncher)”Because this tutorial uses debug interrupt 0x90, you must pass --test-ints.
nipcompile fibonacci.nip -o fibonacci.bincatlaunch run --rom fibonacci.bin --test-intsExpected style of output (decimal + hex per line):
1 0x000000011 0x000000012 0x000000023 0x000000035 0x00000005...