forked from TueHaulund/PendVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fib.pisa
46 lines (41 loc) · 1.68 KB
/
fib.pisa
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
;; pendulum pal file
main: start ; Compute Nth Fibonacci number
addi $1 1 ; $0=0, $1=1, base case
addi $2 18 ; hold N, loop counter
neg $2 ; shorthand for (xori $2 0x1ff) + 1
addi $2 1 ; setup $2 for loop count
addi $3 bottom ; loop target is top; swapped with bottom
addi $4 t1 ; entry point
addi $5 exit ; exit address
call: bez $4 $7 ; call fib routine
rbez $5 $7 ; return from rcall
addi $5 -1
rexit: bez $4 $7
top: bltz $3 $2 ; paired loop branch, could be J
t1: bez $4 $7
addi $7 1
add $0 $1
xor $0 $1 ; swap $0, $1
xor $1 $0
xor $0 $1
addi $2 1 ; increment counter
addi $3 -10 ; correct for exchange
b1: bez $5 $2 ; exit condition
bottom: bltz $3 $2 ; loop until done
exit: bez $5 $2 ; exit point. top and bottom could
; have an unconditional jump
;;; Copy output and call the fib generator in reverse
add $6 $1 ; Result to $6
addi $4 3 ; $4 points to call, adjust to rexit
rcall: rbez $5 $2 ; call backwards; goto b1
bez $5 $7 ; return from rcall
;; reclaim other space
;; we know $0=0, $1=1, $2=-17, $3=bottom,
;; $4=t1, $5=call+1, $6=fib(18), $7=0
;; clear out those values
addi $1 -1
addi $2 17
addi $3 -22
addi $4 -13
addi $5 -9
end: finish