aboutsummaryrefslogtreecommitdiff
path: root/examples/fib.asm
blob: 2864ba3bbc75f640fbf7abd57c20ce4e2e03099a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
;;; fib.asm: A program that generates the fibonacci numbers up to a
;;; very large bound (~UINT64_MAX).  Using the registers to store the
;;; pairs of fibonacci numbers, we ensure only a finite amount of
;;; memory is necessary for this program to function, unlike a pure
;;; stack version.
  ;; Setup initial REG[0] = 1 and REG[1] = 1
  push.word 1
  mov.word 0
  push.word 1
  mov.word 1

  ;; Print REG[0] and REG[1]
  ;; Here is the loop back point `#`
  push.hword 1
  print.hword
  push.byte ':'
  print.char
  push.reg.word 0
  print.word
  push.byte '\n'
  print.char

  push.hword 2
  print.hword
  push.byte ':'
  print.char
  push.reg.word 1
  print.word
  push.byte '\n'
  print.char

  ;;  REG[0] += REG[1]
  push.reg.word 0
  push.reg.word 1
  plus.word
  mov.word 0

  ;;  REG[1] += REG[0]
  push.reg.word 0
  push.reg.word 1
  plus.word
  mov.word 1

  push.word 12200160415121876738
  push.reg.word 1
  gte.word
  ;; Jump to `#`
  jump.if.byte 4
  halt