-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfib.asm
More file actions
80 lines (65 loc) · 885 Bytes
/
fib.asm
File metadata and controls
80 lines (65 loc) · 885 Bytes
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
78
79
80
; this program prints fib(0), fib(1) .. fib(n)
msg: dw "Please enter a number:"
n: dw 0
i: dw 0
@1: dw 1
@2: dw 2
fibResult: dw 0
start:
ldc msg
sout
din
st n
; for (i=0; i<=n; i++) print(fib(i));
loop:
;test if i>n
ld n
sub i
jn finish
; calculate fib(i)
ld i
push
aloc 1 ; alocate space for returned value
; retValue, i
call fib
pop
dout
dloc 1
ldc ','
aout
ld i
add @1
st i
ja loop
finish:
halt
fib:
; if i<=1 return i else return fib(i-1)+fib(i-2)
ld @1
; returnAddress, returnValue, i
subr 2
jzop ret1
ldr 2
sub @1
push
aloc 1
call fib
; retVal, i-1
pop
dloc 1
push ; fib(i-1), retAddr, retVal, i
ldr 3
sub @2
push
aloc 1
call fib
pop
dloc 1
addr 0
str 2 ; retVal = fib(i-1)+fib(i-2)
dloc 1
ret
ret1:
ldr 2
str 1
ret