forked from thuongtruong1009/computer-architecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab4_2_2.s
80 lines (59 loc) · 1.19 KB
/
lab4_2_2.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
#Code by https://github.com/thuongtruong1009
.data
input: .asciiz "Enter n terms: "
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
addi $t5,$t5,1
# loop from $t0 = 0 until n
addi $t0,$zero,1
loop: beq $t0, $t5, exit
jal fibonacci
jal print_output
addi $t0, $t0, 1
j loop
exit:
li $v0, 10
syscall
print_output:
li $v0, 1
move $a0, $t1
syscall
li $v0, 4
la $a0, comma
syscall
jr $31
#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
early_return:
add $t1, $t1, $t4
j recursive
fib_exit:
jr $31