-
Notifications
You must be signed in to change notification settings - Fork 0
/
avgScore.s
251 lines (203 loc) · 5.83 KB
/
avgScore.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
.data
orig: .space 100 # In terms of bytes (25 elements * 4 bytes each)
sorted: .space 100
str0: .asciiz "Enter the number of assignments (between 1 and 25): "
str1: .asciiz "Enter score: "
str2: .asciiz "Original scores: "
str3: .asciiz "Sorted scores (in descending order): "
str4: .asciiz "Enter the number of (lowest) scores to drop: "
str5: .asciiz "Average (rounded up) with dropped scores removed: "
space: .asciiz " "
newLine: .asciiz "\n"
.text
# This is the main program.
# It first asks user to enter the number of assignments.
# It then asks user to input the scores, one at a time.
# It then calls selSort to perform selection sort.
# It then calls printArray twice to print out contents of the original and sorted scores.
# It then asks user to enter the number of (lowest) scores to drop.
# It then calls calcSum on the sorted array with the adjusted length (to account for dropped scores).
# It then prints out average score with the specified number of (lowest) scores dropped from the calculation.
main: #Done
addi $sp, $sp -4
sw $ra, 0($sp)
li $v0, 4
la $a0, str0
syscall # Printing str0
li $v0, 5 # Read the number of scores from user
syscall
move $s0, $v0 # $s0 = numScores
move $t0, $0
la $s1, orig # $s1 = orig
la $s2, sorted # $s2 = sorted
loop_in:
li $v0, 4
la $a0, str1
syscall
sll $t1, $t0, 2
add $t1, $t1, $s1
li $v0, 5 # Read elements from user
syscall
sw $v0, 0($t1)
addi $t0, $t0, 1
bne $t0, $s0, loop_in
move $a0, $s0
jal selSort # Call selSort to perform selection sort in original array
li $v0, 4
la $a0, str2
syscall
move $a0, $s1 # More efficient than la $a0, orig
move $a1, $s0
jal printArray # Print original scores
li $v0, 4
la $a0, str3
syscall
move $a0, $s2 # More efficient than la $a0, sorted
jal printArray # Print sorted scores
li $v0, 4
la $a0, str4
syscall
li $v0, 5 # Read the number of (lowest) scores to drop
syscall
move $a1, $v0
sub $a1, $s0, $a1 # numScores - drop
move $a0, $s2
jal calcSum # Call calcSum to RECURSIVELY compute the sum of scores that are not dropped
# Your code here to compute average and print it
div $v0, $a1 #calcSum / (numScores - drop)
mfhi $t0 #Remainder
mflo $t1 #Answer
move $a0, $t1
bgt $t0, 4, roundup
cont:
move $t1, $v0
move $a0, $v0
#lw $t0, ($t1) #load array ele into t0
li $v0, 1 # |
move $a0, $t1 # |
syscall # Print out element
lw $ra, 0($sp)
addi $sp, $sp 4
li $v0, 10
syscall
# printList takes in an array and its size as arguments.
# It prints all the elements in one line with a newline at the end.
printArray:
# Your implementation of printList here
la $t1, ($a0)#array
add $t3, $zero, $a1#counter
Loop:
beq $t3, $zero, return#if counter == 0
lw $t0, ($t1) #load array ele into t0
li $v0, 1 # |
move $a0, $t0 # |
syscall # Print out element
addi $t3, $t3, -1 #decrease counter
addi $t1, $t1, 4 #move index by four bits
li $v0, 4
la $a0, space
syscall
j Loop
return:
li $v0, 4
la $a0, newLine
syscall
jr $ra
# selSort takes in the number of scores as argument.
# It performs SELECTION sort in descending order and populates the sorted array
selSort:
# Your implementation of selSort here
addi $sp, $sp -8
sw $ra, 4($sp)
sw $s2, 8($sp)
#move $t1, $0
la $t0, ($s1) #OG array
la $t5, ($s2) #COPY
li $t1, 0 #counter
li $t3, 0
loop_start:
lw $t3, ($t0) #load ele of OG array into t3
sw $t3, ($s2) #save t3(OG array ele) into $s2(SORTED array)
addi $t0, $t0, 4 #move index of OG array by four
addi $s2, $s2, 4 #move index of SORTED array by four
addi $t1, $t1, 1 #increase counter by 1
blt $t1, $a0, loop_start #if counter($t1) < size break
Resetting: #Need to reset the SORTED array so that when we print it later it starts at the beginning
add $s2, $t5, $0 #setting SORTED array to the beginnning.
beq $t1, $zero, Resetting
#Setting up for loops
li $t0, 0 # I index
addi $t2, $a0, -1 # size - 1
ILoop: bgt $t0, $t2, End
add $t3, $zero, $t0 # maxindex
addi $t1, $t0, 1 # J index = i + 1
JLoop: bgt $t1, $a0, IEnd
#setting up for greater than compare
sll $t4, $t3, 2
add $s2, $s2, $t4
lw $t5, ($s2) # t5 = SORTED[maxindex]
sub $s2, $s2, $t4 #resetting pos.
sll $t4, $t1, 2 # J * 2 = 4bits
add $s2, $s2, $t4 # SORTED[0] + j
lw $t6, ($s2) # t6 = SORTED[j]
sub $s2, $s2, $t4 #resetting pos.
bgt $t6, $t5, NewMax
addi $t1, $t1, 1
j JLoop
IEnd: # Swapping
sll $t5, $t3, 2 # shifting to get to maxindex location
add $s2, $s2, $t5 # at SORTED[maxindex]
lw $t6, 0($s2) # temp
sub $s2, $s2, $t5 # resetting array back to SORTED[0]
sll $t5, $t0, 2 # shifting to get to i location
add $s2, $s2, $t5 # at SORTED[i]
lw $t7, 0($s2) # t7 = SORTED[i]
sub $s2, $s2, $t5 # resetting array back to SORTED[0]
sll $t5, $t3, 2 #maxindex location
add $s2, $s2, $t5 # SORTED[maxindex]
sw $t7, 0($s2) # SORTED[maxindex] = t7(SORTED[i])
sub $s2, $s2, $t5 # resetting array back to SORTED[0]
sll $t5, $t0, 2 #shifting to SORTED[i]
add $s2, $s2, $t5 # at SORTED[i]
sw $t6, 0($s2) # SORTED[i] = t6(temp)
sub $s2, $s2, $t5 #resetting array back to SORTED[0]
addi $t0, $t0, 1 #increase i
j ILoop
End:
lw $s2, 8($sp)
lw $ra, 4($sp)
addi $sp, $sp 8
jr $ra
NewMax:
add $t3, $t1, $zero # maxindex = j
#addi $t0, $t0, 1
addi $t1, $t1, 1
j JLoop
# calcSum takes in an array and its size as arguments.
# It RECURSIVELY computes and returns the sum of elements in the array.
# Note: you MUST NOT use iterative approach in this function.
calcSum:
pro:
sub $sp,$sp,16
sw $ra,12($sp)
sw $a1,16($sp)
li $v0, 0
#add $t3, $a1, $zero
li $t3, 0
calcSum_callee:
beq $a1, $zero, end_recur
sll $t0, $t3, 2
add $t0, $a0, $t0
lw $t1, ($t0)
add $v0, $t1, $v0
addi $a1, $a1,-1
addi $t3, $t3, 1
j calcSum_callee
end_recur:
lw $a1, 16($sp)
lw $ra, 12($sp)
addiu $sp, $sp, 16
jr $ra
roundup:
addi $v0, $a0, 1
j cont