Repository navigation
Expand file tree
/
Copy pathfibonacci2.m1ps
More file actions
78 lines (74 loc) · 1.49 KB
/
Copy pathfibonacci2.m1ps
File metadata and controls
78 lines (74 loc) · 1.49 KB
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
# Fibonacci - m1ps processor
# MC613 - Laboratory of Digital Systems
# Institute of Computing - State University of Campinas
#
##### Directives #####
#
# The directive width refers to word size in the mif file.
# If it is not explicit the default number is 32.
# It is possible to choose the values 8, 16 and 32.
#$ WIDTH 32
# The directive depth refers to number of words in the mif file.
# Please note that the memory size in bytes is DEPTH*WIDTH/8
# If it is not explicit the default number is 1024.
#$ DEPTH 1024
#
#### End Directives ####
#
#
# Code input:
# r1 receives the n in input.
# r2 receives 0 in the input.
# r3 receives 1 in the input.
# r4 receives memory base address.
# Code output:
# output receives the values of
# fibonacci in memory through r20.
fib:
# Receive the inputs
addi r4, r0, 64
addi r2, r4, -64
addi r3, r4, -63
addi r1, r4, -56
addi r20, r20, -56
# Store 1 in r30
add r30, r0, r3
# Store n in r31.
add r31, r0, r1
# Test if n=0
add r20, r1, r2
# Store in DM(r4) the value of fib(n=0)=0
sw r4, r2
brz end
# Next address
add r4, r4, r30
# Test if n=1
sub r1, r1, r30
sw r4, r3
brz end
loop:
# Next address
add r4, r4, r30
# Calculate r6 = f(n-1) + f(n-2)
# r3 = f(n-1)
# r2 = f(n-2)
add r20, r3, r2
sw r4, r20
# r2 = f(n-1)
add r2, r0, r3
# r3 = r6
add r3, r0, r20
# r1--
sub r1, r1, r30
brz end
brnz loop
end:
# Original address (base + n - n)
sub r4, r4, r31
print:
lw r20, r4
out r20
add r4, r4, r30
sub r31, r31, r30
brnn print
j fib