Repository navigation
Expand file tree
/
Copy pathProject Assignment.txt
More file actions
87 lines (64 loc) · 3.05 KB
/
Copy pathProject Assignment.txt
File metadata and controls
87 lines (64 loc) · 3.05 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
79
80
81
82
83
84
85
86
87
# text output for different functions
.data
prompt: .asciiz "\nEnter a positive number greater than or equal to 25: " #console text for prompt, when the user runs program
illegal: .asciiz "Illegal Number!" #console text for if input is invalid
legal: .asciiz "This number is legal!" #console text for if input is valid
fibResults: .asciiz "\nThe fibonacci sequence of this number is: " #console text for fibonacci sequence calculation
# functions
.text
.globl main
main:
# loop to see if input is legal (greater than/equal to 25)
while:
li $v0, 4 # sys call 4 -> register v0
la $a0, prompt # prompt address -> register a0
syscall # print prompt
li $v0, 5 # sys call 5 -> register v0
syscall # read input
move $t2, $v0 # move v0 -> t2
blt $t2, 25, illegal_input # if input less than 25, it's illegal
#if input is legal
valid:
li $v0, 4 # sys call 4 -> register v0
la $a0, valid # legal address -> register a0
la $a0, legal # legal console text -> register a0
syscall # print text for legal number
# exit main
exit:
li $v0, 4 # sys call 4 -> register v0
la $a0, fibResults # fib seq text -> register a0
syscall # print fib seq text
move $a0, $t2 # move v0 -> a0
jal fibonacci # jump and link to fibonacci function
move $a0, $v0 # move a0 -> v0
li $v0, 1 # sys call 1 -> register v0
syscall # perform system call
li $v0, 10 # sys call 10 -> register v0
syscall # exit program
# Fibonacci function
fibonacci:
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
move $s0, $a0
li $v0, 1 # return value for terminal condition
blez $s0, fibonacciExit # check terminal condition
addi $a0, $s0, -1 # set arguments for recursive call to f(n-1)
jal fibonacci
move $s1, $v0 # save result of f(n-1) to s1
addi $a0, $s0, -2 # set arguments for recursive call to f(n-2)
jal fibonacci
add $v0, $s1, $v0 # add result of f(n-1) to sum
fibonacciExit:
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
jr $ra
# Error text for invalid input
illegal_input:
li $v0, 4 # sys call 4 -> register v0
la $a0, illegal # error text -> register a0
syscall # perform sys call
j while # return to the beginning of the loop