-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab_5_4.s
56 lines (45 loc) · 1.34 KB
/
lab_5_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
.data
x_prompt: .asciiz "Enter the value of x: "
result_msg: .asciiz "e^x = "
newline: .asciiz "\n"
.text
.globl main
main:
# Prompt the user to enter the value of x
li $v0, 4
la $a0, x_prompt
syscall
# Read the value of x
li $v0, 6
syscall
mov.s $f2, $f0 # Move the value of x to $f2
# Initialize variables
li $t0, 8 # Number of terms to use in the Taylor series
li $t1, 1 # Initialize the power of x
li.s $f4, 1.0 # Initialize the result to 1.0
li.s $f5, 1.0 # Initialize the factorial to 1.0
li.s $f6, 1.0 # Initialize the term to be added to 1.0
calc_terms:
# Calculate the next term of the Taylor series
div.s $f6, $f6, $f5 # Calculate x^n / n!
add.s $f4, $f4, $f6 # Add the term to the result
# Update the power of x and the factorial for the next term
addi $t1, $t1, 1 # Increment the power of x
mul.s $f5, $f5, $f31 # Update the factorial (n!)
# Check if more terms need to be calculated
addi $t0, $t0, -1 # Decrement the counter
bnez $t0, calc_terms
# Display the result
li $v0, 4
la $a0, result_msg
syscall
li $v0, 2
mov.s $f12, $f4 # Move the result to $f12 for printing
syscall
# Print newline
li $v0, 4
la $a0, newline
syscall
# Exit
li $v0, 10
syscall