-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab_4_4.s
57 lines (45 loc) · 1.38 KB
/
lab_4_4.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
.text
.globl main
main:
# Prompt the user to enter an integer number
la $a0, prompt
li $v0, 4
syscall # Display "Enter integer number:"
# Read the integer from the user
li $v0, 5
syscall # Read integer
move $a0, $v0 # Pass the integer as an argument to the recursive procedure
# Call the recursive procedure to calculate Fibonacci
jal fibonacci
# Store the return value (nth Fibonacci term) in $a1
move $a1, $v0
# Display end of line
la $a0, endl
li $v0, 4
syscall
# Display the nth Fibonacci term
move $a0, $a1
li $v0, 1
syscall
# Exit the program
li $v0, 10
syscall
# Recursive procedure for Fibonacci calculation
fibonacci:
# Check if n is less than or equal to 1
ble $a0, 1, fibonacci_end # Base case: return n as the Fibonacci term
# Recursive call to compute (n-1)th Fibonacci term
addi $a0, $a0, -1
jal fibonacci
move $s0, $v0 # Store the result of (n-1)th Fibonacci term
# Recursive call to compute (n-2)th Fibonacci term
addi $a0, $a0, -1
jal fibonacci
move $s1, $v0 # Store the result of (n-2)th Fibonacci term
# Compute nth Fibonacci term
add $v0, $s0, $s1
fibonacci_end:
jr $ra # Return to the caller
.data
prompt: .asciiz "Enter integer number: "
endl: .asciiz "\n"