forked from thuongtruong1009/computer-architecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab4_2_1.s
78 lines (58 loc) · 1.22 KB
/
lab4_2_1.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
#Code by https://github.com/thuongtruong1009
.data
description: .asciiz "100 first number is:\n"
comma: .asciiz ", "
.text
.globl main
main:
# print description
li $v0, 4
la $a0, description
syscall
# loop from $t0 = 0 until 100
addi $t0, $zero,1
loop: beq $t0, 11, exit
#jump to fibonacci
jal fibonacci
#jump to print_output
jal print_output
addi $t0, $t0, 1
j loop
#exit the program
exit:
li $v0, 10
syscall
#print the output
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