-
Notifications
You must be signed in to change notification settings - Fork 0
/
MATHforMIPS.asm
161 lines (125 loc) · 3.06 KB
/
MATHforMIPS.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
#MATH for MIPS
.data
INT1: .asciiz "Welcome to MATH for MIPS! What can I help you with?\n1. Sum or subtraction\n2. Multiplication\n3. Division\n4. Power\n5. Factorial\n"
OPT: .word 1,2,3,4,5
STR1: .asciiz "Type two operands, separated by new line character:\n"
STR2: .asciiz "The result is:\n"
STR3: .asciiz "Type one operand:\n"
.text
.globl main
main:
li $v0, 4
la $a0, INT1
syscall
check_arg: li $v0, 5
syscall
slti $t0, $v0, 6
bne $t0, $0, end_check_args
j check_arg
end_check_args:
check_opt: la $t0, OPT
lw $t1, 0($t0)
beq $t1, $v0, sum_sub
lw $t1, 4($t0)
beq $t1, $v0, mult_
lw $t1, 8($t0)
beq $t1, $v0, div_
lw $t1, 12($t0)
beq $t1, $v0, power
lw $t1, 16($t0)
beq $t1, $v0, factorial
sum_sub: li $v0, 4
la $a0, STR1
syscall
li $v0, 5
syscall
add $t0, $0, $v0
li $v0, 5
syscall
add $t1, $0, $v0
li $v0, 4
la $a0, STR2
syscall
add $a0, $t0, $t1
li $v0, 1
syscall
j END
mult_: li $v0, 4
la $a0, STR1
syscall
li $v0, 5
syscall
add $t0, $0, $v0
li $v0, 5
syscall
add $t1, $0, $v0
li $v0, 4
la $a0, STR2
syscall
mul $a0, $t0, $t1
li $v0, 1
syscall
j END
div_: li $v0, 4
la $a0, STR1
syscall
li $v0, 5
syscall
add $t0, $0, $v0
li $v0, 5
syscall
add $t1, $0, $v0
li $v0, 4
la $a0, STR2
syscall
div $a0, $t0, $t1
li $v0, 1
syscall
j END
power: li $v0, 4
la $a0, STR1
syscall
li $v0, 5
syscall
add $t0, $0, $v0
li $v0, 5
syscall
add $t1, $0, $v0
li $t2, 0
li $t3, 1
cycle_power: slt $t4, $t2, $t1
beq $t4, $0, end_cycle_power
mul $t3, $t3, $t0
addiu $t2, $t2, 1
j cycle_power
end_cycle_power: li $v0, 4
la $a0, STR2
syscall
li $v0, 1
addiu $a0, $t3, 0
syscall
j END
factorial: li $v0, 4
la $a0, STR3
syscall
li $v0, 5
syscall
add $t0, $0, $v0
li $t2, 1
li $t3, 1
factorial_power: slt $t4, $t2, $t0
beq $t4, $0, end_factorial_power
slti $t5, $t2, 2
bne $t5, $0, factorial_power1
mul $t3, $t3, $t2
factorial_power1: addiu $t2, $t2, 1
j factorial_power
end_factorial_power: li $v0, 4
la $a0, STR2
syscall
li $v0, 1
addiu $a0, $t3, 0
syscall
j END
END: li $v0, 10
syscall