aboutsummaryrefslogtreecommitdiffstats
path: root/asm/fibo.atk16
diff options
context:
space:
mode:
Diffstat (limited to 'asm/fibo.atk16')
-rw-r--r--asm/fibo.atk1666
1 files changed, 66 insertions, 0 deletions
diff --git a/asm/fibo.atk16 b/asm/fibo.atk16
new file mode 100644
index 0000000..8bb7a6e
--- /dev/null
+++ b/asm/fibo.atk16
@@ -0,0 +1,66 @@
+@opt stack_pointer RF
+@opt csr_scratch RH
+@use ext_std:*
+
+; jump over data segment
+ jpi program
+
+; data segment
+@label ram_offset
+ 0x8000
+
+@label program
+; set up stack pointer to point to beginning of RAM
+ ldi ram_offset RA
+ ldr RA RF
+
+; call fibo subroutine with parameter 10
+ ldi 10 RA
+ csi fibo
+ hlt
+
+@label fibo
+; function fibo
+; parameters:
+; RA n : u16
+; return value:
+; RA fibo(n) : u16
+
+; store RB, RC on stack
+ spu RB
+ spu RC
+; if n < 2, return n
+ subi RA 2 RA
+ bri sign fibo_early
+; store also RD on stack
+ spu RD
+; a = 0
+ ldi 0 RB
+; b = 1
+ ldi 1 RC
+@label fibo_loop
+; v = a + b
+ add RB RC RD
+; a = b
+ mov RC RB
+; b = v
+ mov RD RC
+; n -= 1
+ dec RA
+; loop while n > 0
+ bri zero fibo_done
+ jpi fibo_loop
+@label fibo_early
+; restore used registers
+ spo RC
+ spo RB
+; return from subroutine
+ rsr
+@label fibo_done
+ mov RD RA
+; restore used registers
+ spo RD
+ spo RC
+ spo RB
+; return from subroutine
+ rsr