-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci_sequence.s
123 lines (60 loc) · 3.5 KB
/
fibonacci_sequence.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#final MIPS code to check the valid number in STEP 1 and print the fibonacci numbers for STEP 2
.data #initializing static variables
prompt: .asciiz "Enter an integer (N >= 25): "
error_display: .asciiz "Illegal Number! Please Write valid numbers i.e (N >= 25) \n"
fib_display: .asciiz "Fibonacci Sequence:\n"
.text #writing executable codes
main:
#loop continue until user inputs valid number
j input_loop #using jump instuciton to go to label input_loop
input_loop:
#asking user to input a number
li $v0, 4 #loding into register $v0 that holds system call print i.e 4
la $a0, prompt #loading the address of prompt in $a0 i.e argument for system call
syscall #making a system call to operating system
#reading the input by the user
li $v0, 5 #code 5 = reading input, which is loaded in $v0 using li (loding immediate)
syscall #making system call to OS
move $t0, $v0 # Storeing input in temp register $t0
#checking if number is greater than 25 or not
bge $t0, 25, fibonacci_sequence #bge = branch on greater than or equal , if the number is greater than the immediate value 25 it will call function fibonacci_sequence
# if the number is less than 25 then printing error message
li $v0, 4 #loading print in $v0
la $a0, error_display #loading the address of error message
syscall
j input_loop #calling loop for user to input right number
#startring fibonacci_sequence
fibonacci_sequence:
#initilizing variable for fibonacci_sequence
li $t1, 0 # loading First Fibonacci number to temp register $t1
li $t2, 1 # loading Second Fibonacci number to temp register $t2
li $t3, 0 # loading Counter to keep track of number in fibonacci_sequence
#printing Fibonacci messages
li $v0, 4 #loading print code in $v0
la $a0, fib_display #loading the address of displaying Fibonacci sequence
syscall
fibonacci_loop:
# Printing the current Fibonacci number
li $v0, 1 # loading 1 to $v0 , indicating the print integer code = 1
move $a0, $t1 # Loading the current Fibonacci number into $a0
syscall # Performing the system call to print the integer in $a0
#calclating the next fibonacci_sequence
add $t4, $t1, $t2 # $t4 = $t1 + $t2, adding last two values to create new fibonacci number
move $t1, $t2 # shifing values, $t2 will be stored in $t1
move $t2, $t4 # shifing values, $t4(NEW NUMBER) will be stored in $t2
addi $t3, $t3, 1 # Incrementing the counter of the currect fib_display
# Printing a space between the Fibonacci numbers for better Readability
li $v0, 11 #11 = system call for printing character
li $a0, 32 # ASCII code for space
syscall
# Checking if the counter reached the value inputed by the user
bge $t3, $t0, exit_code # if counter >= N then go to exit command
j fibonacci_loop #jumping to the starting of loop until we meet the criteria
exit_code:
#priting the last number of fibonacci sequence before exiting
li $v0, 1 # loading 1 to $v0 , indicating the print integer code = 1
move $a0, $t1 # Loading the current Fibonacci number into $a0
syscall # Performing the system call to print the integer in $a0
# Exiting the program once we reach N
li $v0, 10 #loading system call for program exit
syscall ##making system call to OS and exiting