-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-1.asm
192 lines (180 loc) · 6 KB
/
test-1.asm
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#-------------------------------- PROJECT --------------------------------#
# USING METHODS IN FINDING INTERGRAL AREA
# ASSIGN FLOATING DATA #
.data
Message_Choose_Method: .asciiz "Choose the method of calculation (Trapezoid = 0 / Simpson = 1): "
Message_b: .asciiz "Input b (b > 0): "
Message_n: .asciiz "Input n (10 <= n <= 20): "
Message_result: .asciiz "The integral area of the function is: "
const1: .float 1.0
const2: .float 2.0
const3: .float 3.0
const4: .float 4.0
.text
#-------------------------------- Get_data -------------------------------#
get_method:
li $v0, 51
la $a0, Message_Choose_Method
syscall
add $t1, $a0, $zero # range in $s1
get_range:
li $v0, 52
la $a0, Message_b
syscall
mov.s $f13, $f0 # range in $f13
cvt.w.s $f14, $f13 # convert from float to word
mfc1 $t8, $f14
blt $t8, 0, get_range # branch for <0
get_number_of_rec:
li $v0, 51
la $a0, Message_n
syscall
add $s2, $a0, $zero # step in $s2
bgt $s2, 20, get_number_of_rec # branch for >20
blt $s2, 10, get_number_of_rec # branch for <10
#--------------------------------- Main ----------------------------------#
main:
beq $t1,0,calculate_integral_Trapezoid # using Trapezoid method
nop
beq $t1,1,calculate_integral_Simpson # using Simpson method
nop
#--------------------------------- Print ---------------------------------#
print_and_quit:
print: li $v0, 2
syscall
li $v0, 57
la $a0, Message_result
syscall
quit: li $v0, 10
syscall
#-------------------------- Calculate function ---------------------------#
calculate_function:
sw $fp, -4($sp) # Save frame pointer
addi $fp, $sp,0 # New frame pointer point to stack's top
addi $sp, $sp,-12 # Allocate space for $fp,$ra,$f0 in stack
sw $ra, 4($sp) # Save return address
s.s $f0, 0($sp) # Save f0
mov.s $f0, $f13 # f0 = x
mul.s $f0, $f0, $f0 # f0 = x*x
l.s $f12, const1 # f12 = 1
add.s $f0, $f0, $f12 # f0 = x*x + 1
l.s $f12, const4 # f12 = 4
div.s $f12, $f12, $f0
lw $ra, 4($sp) # Restore return address
l.s $f0, 0($sp) # Restore f0
addi $sp, $fp, 0 # Restore stack pointer
lw $fp, -4($sp) # Restore frame pointer
jr $ra # Jump to calling
#------------------------------- Trapezoid -------------------------------#
calculate_integral_Trapezoid:
sw $fp, -4($sp) # save frame pointer
addi $fp, $sp, 0 # new frame pointer point to the top
addi $sp, $sp, -36 # adjust stack pointer
sw $ra, 28($sp) # save return address
sw $s0, 24($sp) # save s0
s.s $f0, 20($sp) # Save f0
s.s $f1, 16($sp) # Save f1
s.s $f2, 12($sp) # Save f2
s.s $f3, 8($sp) # Save f3
s.s $f4, 4($sp) # Save f4
s.s $f5, 0($sp) # Save f5
move $s0, $s2 # s0 = n
mov.s $f0, $f13 # f0 = b
mtc1 $s2, $f1 # f1 = n
# cvt.s.w $f0, $f0 # convert b to float
cvt.s.w $f1, $f1 # convert n to float
div.s $f2, $f0, $f1 # f2 = delta_x
mtc1 $zero, $f3 # f3 = x = 0
xor $t0, $t0, $t0 # count = 0
l.s $f4, const4 # f4 = sum = f(0) = 4.0
l.s $f5, const2 # f5 = 2.0
loop:
add.s $f3, $f3, $f2 # x = x + delta_x
addi $t0, $t0, 1 # count++
beq $t0, $s0, end_loop # if count == n, end loop
mov.s $f13, $f3 # Set parameter
jal calculate_function # f12 = f(x)
nop
add.s $f12, $f12, $f12 # Get the value of 2*f(x)
add.s $f4, $f4, $f12 # sum = sum + 2*f(x)
j loop
end_loop:
mov.s $f13, $f3 # Set parameter
jal calculate_function # f12 = f(x)
add.s $f4, $f4, $f12 # sum = sum + f(x)
# result = sum * delta_x / 2
mul.s $f4, $f4, $f2
div.s $f12, $f4, $f5
lw $ra, 28($sp) # Restore return address
lw $s0, 24($sp) # Restore s0
l.s $f0, 20($sp) # Restore f0
l.s $f1, 16($sp) # Restore f1
l.s $f2, 12($sp) # Restore f2
l.s $f3, 8($sp) # Restore f3
l.s $f4, 4($sp) # Restore f4
l.s $f5, 0($sp) # Restore f5
addi $sp, $fp, 0 # Return stack pointer
lw $fp, -4($sp) # Return frame pointer
j print_and_quit # Return to where called the function
#-------------------------------------------------------------------------#
#-------------------------------- Simpson --------------------------------#
calculate_integral_Simpson:
sw $fp, -4($sp) # save frame pointer
addi $fp, $sp, 0 # new frame pointer point to the top
addi $sp, $sp, -36 # adjust stack pointer
sw $ra, 28($sp) # save return address
sw $s0, 24($sp) # save s0
s.s $f0, 20($sp) # Save f0
s.s $f1, 16($sp) # Save f1
s.s $f2, 12($sp) # Save f2
s.s $f3, 8($sp) # Save f3
s.s $f4, 4($sp) # Save f4
s.s $f5, 0($sp) # Save f5
move $s0, $s2 # s0 = n
mov.s $f0, $f13 # f0 = b
mtc1 $s2, $f1 # f1 = n
# cvt.s.w $f0, $f0 # convert b to float
cvt.s.w $f1, $f1 # convert n to float
div.s $f2, $f0, $f1 # f2 = delta_x
mtc1 $zero, $f3 # f3 = x = 0
xor $t0, $t0, $t0 # count = 0
l.s $f4, const4 # f4 = sum = f(0) = 4.0
l.s $f5, const2 # f5 = 2.0
loop2:
add.s $f6, $f3, $f2
mov.s $f13, $f6
jal calculate_function
nop
l.s $f7, const4
mul.s $f12, $f12, $f7
add.s $f4, $f4, $f12
add.s $f3, $f3, $f2
add.s $f3, $f3, $f2 # x = x + 2 * delta_x
addi $t0, $t0, 2 # count++
beq $t0, $s0, end_loop2 # if count == n, end loop
mov.s $f13, $f3 # Set parameter
jal calculate_function # f12 = f(x)
nop
add.s $f12, $f12, $f12 # Get the value of 2*f(x)
add.s $f4, $f4, $f12 # sum = sum + 2*f(x)
j loop2
end_loop2:
mov.s $f13, $f3 # Set parameter
jal calculate_function # f12 = f(x)
add.s $f4, $f4, $f12 # sum = sum + f(x)
# result = sum * delta_x / 3
mul.s $f4, $f4, $f2
l.s $f6, const3
div.s $f12, $f4, $f6
lw $ra, 28($sp) # Restore return address
lw $s0, 24($sp) # Restore s0
l.s $f0, 20($sp) # Restore f0
l.s $f1, 16($sp) # Restore f1
l.s $f2, 12($sp) # Restore f2
l.s $f3, 8($sp) # Restore f3
l.s $f4, 4($sp) # Restore f4
l.s $f5, 0($sp) # Restore f5
addi $sp, $fp, 0 # Return stack pointer
lw $fp, -4($sp) # Return frame pointer
j print_and_quit
#-------------------------------------------------------------------------#