Files
damn_simple_architecture/resources/dsa/fib.dsa
T
2025-06-17 23:50:16 +01:00

35 lines
1.1 KiB
Plaintext

// include print "../resources/dsa/print.dsa"
// Fibonacci sequence calculator in DSA assembly
// Calculates the first 8 Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13
dw fib_count: 6 // How many more numbers to calculate after F(0) and F(1)
init:
// Initialize the first two Fibonacci numbers
lli rg0, 0 // F(0) = 0
lli rg1, 1 // F(1) = 1
push rg0
// Load loop counter
ldw fib_count, zero, rg2 // Load number of iterations remaining
fibonacci_loop:
// Calculate next Fibonacci number: F(n) = F(n-1) + F(n-2)
add rg0, rg1, rg4 // rg4 = rg0 + rg1 (new Fibonacci number)
// Shift the sequence forward
mov rg1, rg0 // rg0 = previous rg1 (F(n-2) = F(n-1))
mov rg4, rg1 // rg1 = rg4 (F(n-1) = F(n))
// Decrement loop counter
dec rg2 // rg2 = rg2 - 1
// Check if we should continue looping
cmp rg2, zero // Compare counter with 0
jgt fibonacci_loop // Jump back if counter > 0
finish:
mov rg1, acc // Final Fibonacci number is in acc
hlt
jmp print::run