-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fibonacci.asm
73 lines (48 loc) · 1.31 KB
/
Fibonacci.asm
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
.data
msg_input: .asciiz "Please press the goal index:"
msg_done: .asciiz "The result is:"
.text
main_start:
#initialize
and $s2, $s2, $zero # $s2 = Fibonacci[1]
and $s3, $s3, $zero
addi $s3, $s3, 1 # $s3 = Fibonacci[2]
#print message
li $v0, 4
la $a0, msg_input
syscall
#get the index
li $v0, 5
syscall
and $s4, $s4, $zero
add $s4, $s4, $v0
#initialize index iterator
and $t1, $t1, $zero
addi $t1, $t1, 2 # i = 2
do:
#set index
addi $t1, $t1, 1 # i++
#calculate Fibonacci[3]
add $t0, $s2, $s3 # $t0 = Fibonacci[3] = Fibonacci[1] + Fibonacci[2]
and $s2, $s2, $zero
add $s2, $s2, $s3
and $s3, $s3, $zero
add $s3, $s3, $t0
check_done:
sltu $t2, $t1, $s4
beq $t2, $zero, done # if $t2 == $zero then done
j do # jump to do
done:
#print message
li $v0, 4
la $a0, msg_done
syscall
and $s1, $s1, $zero
add $s1, $s1, $t0
and $a0, $a0, $zero
add $a0, $a0, $t0
li $v0, 1 # $v0 = 1
syscall
#over
li $v0, 10
syscall