-
Notifications
You must be signed in to change notification settings - Fork 4
/
bm_fib.tas
78 lines (63 loc) · 1.65 KB
/
bm_fib.tas
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Conventions:
# O is the stack pointer (post-decrement)
# B is the (so far only) return register
# C is the (so far only) argument register
# N is the relative-jump temp register
.set VGA_ROWS, 32
.set VGA_COLS, 64
_start:
c <- 1 # argument
o <- ((1 << 13) - 1)
j <- 0 # row (0 - 31)
k <- 0 # column (0 - 3)
loop:
l <- k * 16 # column (0 - 63)
# compute fib(N)
[o] <- c ; o <- o - 1
[o] <- p + 2 ; o <- o - 1 ; p <- @+fib + p
o <- o + 1 ; c <- [o]
c -> [0x100] # write N to LED
#c -> [0x101]
# write N to VGA
[o] <- c ; o <- o - 1
d <- j
e <- l
f <- 2
[o] <- p + 2 ; o <- o - 1 ; p <- @+putnum + p
o <- o + 1 ; c <- [o]
# write fib(N) to VGA
[o] <- c ; o <- o - 1
c <- b
d <- j
e <- l + 4
f <- 12
[o] <- p + 2 ; o <- o - 1 ; p <- @+putnum + p
o <- o + 1 ; c <- [o]
c <- c + 1 # increment N
j <- j + 1 # increment row
m <- j >= @VGA_ROWS # check for column full
# TODO try `l <- m * -20 + l`
k <- k - m # if j >= @VGA_ROWS, k <- k - -1
j <- j &~ m # if j >= @VGA_ROWS, j <- j & 0
p <- p + @+loop
illegal
fib:
d <- 1
d <- d < c # zero or one ?
p <- @+_recurse & d + p # not-zero is true (c >= 2)
b <- c
o <- o + 1
p <- [o]
_recurse:
[o] <- c ; o <- o - 1
c <- c - 1
[o] <- p + 2 ; o <- o - 1 ; p <- @+fib + p
o <- o + 1 ; c <- [o]
[o] <- b ; o <- o - 1
c <- c - 2
[o] <- p + 2 ; o <- o - 1 ; p <- @+fib + p
d <- b
o <- o + 1 ; b <- [o]
b <- d + b
o <- o + 1
p <- [o]