Created
March 8, 2020 23:43
-
-
Save Arham4/dc930dba7f160d5b87a5bc5873e0e48f to your computer and use it in GitHub Desktop.
SE 3340: Computer Architecture - Homework 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.data | |
buffer: .space 80 | |
numbers: .space 80 | |
inputFileName: .asciiz "input.txt" | |
messageBefore: .asciiz "The array before: " | |
messageAfter: .asciiz "\nThe array after: " | |
meanMessage: .asciiz "\nThe mean is: " | |
medianMessage: .asciiz "\nThe median is: " | |
standardOfDeviationMessage: .asciiz "\nThe standard of deviation is: " | |
delimeter: .asciiz " " | |
newLine: .asciiz "\n" | |
mean: .float 0.0 | |
.text | |
la $a0, inputFileName | |
la $a1, buffer | |
jal readText | |
ble $v0, $zero, exit | |
la $a0, numbers | |
li $a1, 20 | |
la $a2, ($s0) | |
jal extractNumbers | |
la $a0, numbers | |
li $a1, 20 | |
la $a2, messageBefore | |
la $a3, delimeter | |
jal printNumbers | |
la $a0, numbers | |
li $a1, 20 | |
jal sortNumbers | |
la $a0, numbers | |
li $a1, 20 | |
la $a2, messageAfter | |
la $a3, delimeter | |
jal printNumbers | |
la $a0, numbers | |
li $a1, 20 | |
jal calculateMean | |
swc1 $f0, mean | |
# print out the mean | |
li $v0, 4 | |
la $a0, meanMessage | |
syscall | |
li $v0, 2 | |
lwc1 $f12, mean | |
syscall | |
la $a0, numbers | |
li $a1, 20 | |
jal calculateMedian | |
# print out the median | |
li $v0, 4 | |
la $a0, medianMessage | |
syscall | |
beq $v1, 1, printOutMedianFloat | |
printOutMedianInteger: | |
li $v0, 4 | |
la $a0, ($v0) | |
syscall | |
j continueMain | |
printOutMedianFloat: | |
li $v0, 2 | |
mtc1 $zero, $f1 | |
add.s $f12, $f0, $f1 | |
syscall | |
j continueMain | |
continueMain: | |
la $a0, numbers | |
li $a1, 20 | |
lwc1 $f1, mean | |
jal calculateStandardOfDeviation | |
# print out the standard of deviation | |
li $v0, 4 | |
la $a0, standardOfDeviationMessage | |
syscall | |
li $v0, 2 | |
mtc1 $zero, $f1 | |
add.s $f12, $f0, $f1 | |
syscall | |
exit: | |
li $v0, 10 | |
syscall | |
# functions | |
printNewLine: | |
li $v0, 4 | |
la $a0, newLine | |
syscall | |
jr $ra | |
readText: # $a0 - address of file name, $a1 - address of buffer where data is stored | |
# RETURNS: $v0 - the amount of bytes read | |
# open file for reading | |
move $s0, $a1 # keep address of buffer | |
li $v0, 13 | |
li $a1, 0 | |
li $a2, 0 | |
syscall | |
move $s1, $v0 # save file descriptor in $s1 | |
# reading from file | |
li $v0, 14 | |
move $a0, $s1 | |
la $a1, ($s0) | |
li $a2, 80 | |
syscall | |
jr $ra | |
extractNumbers: # $a0 - where to store numbers, $a1 - the amount of numbers, $a2 - address where the buffer starts | |
la $s0, ($a0) # $s0 is where $a0 was | |
la $s1, ($a2) # $s1 is where $a2 was | |
li $t0, 0 # $t0 will keep track of the current number | |
li $t1, 0 # $t1 will keep track of the current digit | |
li $t4, 0 # $t4 will keep track of how many numbers have been processed | |
loopNumbers: | |
beq $t4, $a1, extractExit | |
loopNumber: | |
lb $t1, ($s1) | |
beq $t1, 0 extractExit | |
beq $t1, 10, nextNumber | |
blt $t1, 48, nextDigit | |
bgt $t1, 57, nextDigit | |
subi $t1, $t1, 48 # standardize ascii value to digit | |
sll $t2, $t0, 1 | |
sll $t3, $t0, 3 | |
add $t0, $t2, $t3 # multiply $t0 by 10 | |
add $t0, $t0, $t1 # add the number read $t0 | |
nextDigit: | |
addi $s1, $s1, 1 # go to next digit | |
j loopNumber | |
nextNumber: | |
sw $t0, ($s0) | |
addi $s0, $s0, 4 | |
addi $s1, $s1, 1 | |
addi $t4, $t4, 1 | |
li $t0, 0 | |
j loopNumbers | |
extractExit: | |
jr $ra | |
printNumbers: # $a0 - the address of where the numbers start, $a1 - how many numbers to print, $a2 - message before printing, $a3 - delimeter | |
la $s0, ($a0) | |
# print message before numbers | |
li $v0, 4 | |
la $a0, ($a2) | |
syscall | |
li $t0, 0 # $t0 will keep track of how many numbers are printed | |
loop: | |
beq $t0, $a1, exit2 | |
li $v0, 1 | |
lw $a0, ($s0) | |
syscall | |
li $v0, 4 | |
la $a0, ($a3) | |
syscall | |
addi $s0, $s0, 4 | |
addi $t0, $t0, 1 | |
j loop | |
exit2: | |
jr $ra | |
sortNumbers: # $a0 - the address of where the numbers start. $a1 - how many numbers there are | |
move $s0, $a1 # $s0 = count - 1 | |
sub $s0, $s0, 1 | |
li $t0, 0 | |
forLoop1: | |
beq $t0, $s0, exitSort | |
move $t1, $t0 | |
move $t2, $t0 | |
forLoop2: | |
add $t2, $t2, 1 | |
bne $t2, $a1, compare | |
j swap | |
compare: | |
li $t3, 4 | |
mul $t4, $t3, $t2 | |
add $t4, $t4, $a0 | |
mul $t5, $t3, $t1 | |
add $t5, $t5, $a0 | |
lw $t6, ($t4) | |
lw $t7, ($t5) | |
bge $t6 $t7, forLoop2 | |
move $t1, $t2 | |
j forLoop2 | |
swap: | |
li $t3, 4 | |
mul $t4, $t3, $t0 | |
add $t4, $t4, $a0 | |
lw $t6, ($t4) | |
mul $t5, $t3, $t1 | |
add $t5, $t5, $a0 | |
lw $t7, ($t5) | |
sw $t6, ($t5) | |
sw $t7, ($t4) | |
add $t0, $t0, 1 | |
j forLoop1 | |
exitSort: | |
jr $ra | |
calculateMean: # $a0 - the address of the numbers, $a1 - the amount of numbers | |
# RETURNS $f0 - the mean | |
la $s0, ($a0) # $s0 is where $a0 was | |
li $t0, 0 # $t0 will keep track of the current number count | |
li $t1, 0 # $t1 will keep track of the current sum | |
li $v0, 0 # $v0 will be the mean | |
loopMean: | |
beq $t0, $a1, exitMean | |
lw $t2, ($s0) # $t2 is the current number we are at | |
add $t1, $t1, $t2 | |
add $s0, $s0, 4 | |
add $t0, $t0, 1 | |
j loopMean | |
exitMean: | |
mtc1 $t1, $f1 | |
cvt.s.w $f1, $f1 | |
mtc1 $a1, $f2 | |
cvt.s.w $f2, $f2 | |
div.s $f0, $f1, $f2 | |
jr $ra | |
calculateMedian: # $a0 - the address of the numbers, $a1 - the amount of numbers | |
# RETURNS $v1 - 1 if float, 0 if integer | |
# if float: $f0 - the median | |
# if integer: $v0 - the median | |
la $s0, ($a0) # $s0 has a copy of the address | |
li $t1, 1 # $t1 is for the number 1 | |
andi $t0, $a1, 0x01 # if this and operation returns 1 in $t0, it is odd, else even | |
sll $a1, $a1, 2 | |
beq $t0, $t1, calculateMedianIfOdd | |
calculateMedianIfEven: | |
li $t1, 2 # now $t1 is for the number 2 | |
srl $t2, $a1, 1 # $t2 is the first number index | |
subi $t2, $t2, 4 | |
add $s0, $s0, $t2 | |
lwc1 $f1, ($s0) | |
cvt.s.w $f1, $f1 | |
lwc1 $f2, 4($s0) | |
cvt.s.w $f2, $f2 | |
mtc1 $t1, $f3 | |
cvt.s.w $f3, $f3 | |
add.s $f0, $f1, $f2 | |
div.s $f0, $f0, $f3 | |
li $v1, 1 | |
j exitMedian | |
calculateMedianIfOdd: | |
srl $t2, $a1, 2 # $t2 is the middle index | |
add $s0, $s0, $t2 | |
lw $v0, ($s0) | |
li $v1, 0 | |
j exitMedian | |
exitMedian: | |
jr $ra | |
calculateStandardOfDeviation: # $a0 - the address of the numbers, $a1 - the amount of numbers, $f1 - the mean | |
# RETURNS $f0 - the standard of deviation | |
la $s0, ($a0) # $s0 has a copy of the address | |
mtc1 $zero, $f0 # $f0 is our answer | |
cvt.s.w $f0, $f0 | |
li $t0, 0 # $t0 will be the counter for numbers | |
loopStandardOfDeviation: | |
beq $t0, $a1, calculateRadicand | |
lwc1 $f2, ($s0) # $f2 will be the number we are at, eventually conforming to what is inside the summation function | |
cvt.s.w $f2, $f2 | |
sub.s $f2, $f2, $f1 | |
mul.s $f2, $f2, $f2 | |
add.s $f0, $f0, $f2 | |
addi $s0, $s0, 4 | |
addi $t0, $t0, 1 | |
j loopStandardOfDeviation | |
calculateRadicand: | |
move $t1, $a1 # $t1 will represent the amount of numbers minus 1 | |
subi $t1, $t1, 1 | |
mtc1 $t1, $f3 # $f3 will represent the amount of numbers minus 1 in a float-point | |
cvt.s.w $f3, $f3 | |
div.s $f0, $f0, $f3 | |
# finally, calculate the standard of deviation | |
sqrt.s $f0, $f0 | |
jr $ra | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment