forked from thuongtruong1009/computer-architecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab4_2_3.s
87 lines (65 loc) · 1.26 KB
/
lab4_2_3.s
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
81
82
83
84
85
86
87
#Code by https://github.com/thuongtruong1009
.data
input: .asciiz "Input n terms: "
output:
comma: .asciiz ", "
.text
.globl main
main:
# print input
li $v0, 4
la $a0, input
syscall
#load n times
li $v0, 5
syscall
move $t5,$v0
beq $t5,1,return_1
blt $t5,1 return_1
# loop from $t0 = 0 until n
addi $t0,$zero,1
addi $t6,$zero,1
loop: beq $t0, $t5, exit
jal fibonacci
jal sum
addi $t0, $t0, 1
j loop
exit:
li $v0, 1
move $a0, $t6
syscall
li $v0, 10
syscall
sum:
add $t6,$t6,$t1
#compute fibonacci series
fibonacci:
move $t1, $0
move $t2, $sp
li $t3, 1
addi $sp, $sp, -4 # push initial $t0 on stack
sw $t0, 0($sp)
#perform recursive
recursive:
beq $sp, $t2, fib_exit # if stack is empty, exit
lw $t4, 0($sp) # pop next $t4 off stack
addi $sp, $sp, 4
bleu $t4, $t3, early_return
sub $t4, $t4, 1 # push $t4 - 1 on stack
addi $sp, $sp, -4
sw $t4, 0($sp)
sub $t4, $t4, 1 # push $t4 - 2 on stack
addi $sp, $sp, -4
sw $t4, 0($sp)
j recursive
return_1:
li $v0, 1
addi $a0,$zero,1
syscall
li $v0, 10
syscall
early_return:
add $t1, $t1, $t4
j recursive
fib_exit:
jr $31