Question
MIPS For this program, write a routine which calculates the dot product of two vectors. The accompanying boilerplate code should begin your project. Do not
MIPS
For this program, write a routine which calculates the dot product of two vectors. The accompanying boilerplate code should begin your project. Do not change anything in the boilerplate code when you turn in the assignment. If would be safe to simply replace it if you made modifications for testing purposes. You are absolutely welcome to study the boilerplate code for ideas on how to write your code, but this isnt mandatory.
The subroutine is named Dot and is found at the end of the boilerplate code. The base addresses of the two vectors are in $a0 and $a1. The length of the vectors is in $a2. Place the return value of your subroutine in $v0. The correct answer for the given vectors is 6060 (decimal).
#DO NOT CHANGE ANY CODE BELOW THIS COMMENT
.data
Separator:
.asciiz ", "
crlf:
.asciiz " "
V1Name:
.asciiz "Vector 1: "
V2Name:
.asciiz "Vector 2: "
DotName: "Dot Product:"
VLength:
.word 0x12
Vector1:
.word 39, 11, 4, 7, 17, 9, 24, 11, 4, 2, 28, 37, 13, 7, 5, 2, 45, 49
Vector2:
.word 6, 17, 39, 3, 19, 25, 14, 33, 25, 43, 5, 28, 36, 47, 23, 31, 8, 31
.text
la $a0, V1Name
addi $v0, $0, 4
syscall
la $a0, Vector1
lw $a1, VLength
jal PrintVector
la $a0, V2Name
addi $v0, $0, 4
syscall
la $a0, Vector2
lw $a1, VLength
jal PrintVector
la $a0, DotName
addi $v0, $0, 4
syscall
la $a0, Vector1
la $a1, Vector2
lw $a2, VLength
jal Dot
addi $a0, $v0, 0
addi $v0, $0, 1
syscall
la $a0, crlf
addi $v0, $0, 4
syscall
addi $v0, $0, 10
syscall
PrintVector:
# $a0 - address of vector
# $a1 - number of elements
# $t0 - copy of $a0 since we have to overwrite it to do a syscall
# $t1 - manipulatable copy of $a0
# $t2 - loo counter
addi $t0, $a0, 0
addi $t1, $a0, 0
addi $t2, $0, 0 #initialize counter
PrintLoop:
lw $a0, 0($t1)
addi $v0, $0, 1
syscall
addi $t2, $t2, 1
beq $t2, $a1, PrintDone
addi $t1, $t1, 4
la $a0, Separator
addi $v0, $0, 4
syscall
j PrintLoop
PrintDone:
la $a0, crlf
addi $v0, $0, 4
syscall
addi $a0, $t0, 0 # restore $a0's original value
jr $ra
#DO NOT CHANGE ANY CODE ABOVE THIS COMMENT
#BELOW IS MY ATTEMPT AT THE PROJECT - CAN YOU PLEASE ASSIST ME IN ANY MISTAKES
Dot:
# $a0 - address of vector 1
# $a1 - address of vector 2
# $a2 - number of elements
# $v0 - dot product
# your code goes here
#load word Vector1 and Vector2
lw $t0, 18, ($a0) #load address Vector1
lw $t1, 18, ($a1) #load address Vector2
addi $v0, $0, 0
# loop through the number of elements
loop:
add $v0 , $v0, $t0 #add contect of Vectors to $t0
addi $t0, $t0, 1 #increment
bne $t0, $0, loop #return to loop
mul $t2, $t0, $t1 #multiply Vector1 by Vector2
add $t3, $t3, $t2 #add multiplication Vector1 to Vector2
jr $ra #return to boilerpoint
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started