#Copyright 2021 by John Bowman #Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), #to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, #and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: #The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, #WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #This program looks for the smallest distance between two sets of coordinates #using the distance formula: d = sqrt((xj-xi)^2 + (yj-yi)^2 + (zj-zi)^2) #and outputs the smallest distance along with the coordinates #that produce the smallest distance #Developed using MARS 4.5 MIPS Assembler and Runtime Simulator .data message: .asciiz "The pairs of coordinates that are closest to each other are " message2: .asciiz "\nThe square root of these coordinates is: " space: .asciiz " " openPar: .asciiz "(" closePar: .asciiz ")" comma: .asciiz "," precision: .float 0.005 numbers: .float #Float array. Every two rows represents a set of data. i.e. x1,y1,z1 - x2,y2,z2 -86.197052961689, -11.611577927042, 61.992590409186, 13.27904907337, 19.916714939069, -20.722319227382, -4.2258863849783, 45.297465287969, -90.569809463764, -95.944184921756, 47.662103394222, 64.51404880929, -21.807443326017, -24.698308698634, 75.762861069285, -36.6654951049497, 43.494575924847, 86.447702712523, 47.778522103541, -65.538547015254, 59.179507921132, 54.530144848978, -67.933306319424, -36.693280039158, 84.306270676353, 21.53150037385, 44.934044045448, 73.659364837401, 81.085378856605, -73.1797878870 .text lwc1 $f8, precision #Load precision into $f8 #Initialize counter variables li $t0, 0 li $t1, 5 #Initialize register $f10, the register selected for storing the smallest square root #Assign very large number because we need the first square root generated to be smaller than this number #Otherwise, branching will fail la $a0, 40000000 mtc1 $a0, $f10 cvt.s.w $f10, $f10 startLoop: beq $t0,$t1,exitLoop #Terminate after five iterations nop la $a0, numbers mul $t3, $t0, 12 add $a0, $a0, $t3 l.s $f0, 0($a0) #xi l.s $f1, 4($a0) #yi l.s $f2, 8($a0) #zi la $a0, numbers mul $t3, $t0, 12 add $a0, $a0, $t3 l.s $f3, 0($a0) #xj l.s $f4, 4($a0) #yj l.s $f5, 8($a0) #zj mov.s $f18, $f0 mov.s $f19, $f1 mov.s $f20, $f2 mov.s $f21, $f3 mov.s $f22, $f4 mov.s $f23, $f5 sub.s $f3, $f3, $f0 #xj - xi sub.s $f4, $f4, $f1 #yj - yi sub.s $f5, $f5, $f2 #zj - zi mul.s $f3, $f3, $f3 #square value of xj-xi mul.s $f4, $f4, $f4 #square value of yj-yi mul.s $f5, $f5, $f5 #square value of zj-zi #Add results together add.s $f3, $f3, $f4 add.s $f3, $f3, $f5 mov.s $f0, $f3 #Compute square root with Newton-Raphson approximation dowhile: mul.s $f6, $f3, $f3 sub.s $f6, $f6, $f0 add.s $f7, $f3, $f3 div.s $f6, $f6, $f7 sub.s $f3, $f3, $f6 c.lt.s $f6, $f8 bc1f dowhile nop addi $t0,$t0,1 #Increment looping value #If a smaller square root is found, store it and its coordinates by branching to continue label c.lt.s $f3, $f10 bc1t continue nop j startLoop #Unconditionally jump to beginning of loop nop #When a smaller square root is found, store it and the coordinates that generated it #Return to beginning of startLoop continue: mov.s $f10, $f3 mov.s $f11, $f18 #x1 mov.s $f13, $f19 #y1 mov.s $f14, $f20 #z1 mov.s $f15, $f21 #x2 mov.s $f16, $f22 #y2 mov.s $f17, $f23 #z2 j startLoop #Uncontionally jump to beginning of startLoop nop #Print results exitLoop: #print pairs header la $a0, message li $v0, 4 syscall la $a0, openPar li $v0, 4 syscall mov.s $f12, $f11 li $v0, 2 syscall la $a0, comma li $v0, 4 syscall mov.s $f12, $f15 li $v0, 2 syscall la $a0, closePar li $v0, 4 syscall la $a0, space li $v0, 4 syscall la $a0, openPar li $v0, 4 syscall mov.s $f12, $f13 li $v0, 2 syscall la $a0, comma li $v0, 4 syscall mov.s $f12, $f16 li $v0, 2 syscall la $a0, closePar li $v0, 4 syscall la $a0, space li $v0, 4 syscall la $a0, openPar li $v0, 4 syscall mov.s $f12, $f14 li $v0, 2 syscall la $a0, comma li $v0, 4 syscall mov.s $f12, $f17 li $v0, 2 syscall la $a0, closePar li $v0, 4 syscall #Print message for smallest square root la $a0, message2 li $v0, 4 syscall #Print value of smallest square root mov.s $f12, $f10 li $v0, 2 syscall #Terminate program li $v0, 10 syscall