Develop a MIPS assembly program named VectorMult.asm that reads in one or more vectors of a specified
Question:
Develop a MIPS assembly program named VectorMult.asm that reads in one or more vectors of a specified number of dimensions. Each element of a vector is a 32-bit integer word. The vectors are read and stored in main memory. The indexes of any two vectors can be entered and the program prints the element-wise multiplication of the vectors. For example, if the vectors (1, 2, 3) and (4, 5, 6) are read in, multiplying vector 0 and vector 1 results in the program printing (4, 10, 18).
Further details appear below. We strongly suggest you implement and thoroughly test each part before proceeding to the next part.
Part 1: Initial input.
Your program should start by reading two integers. The first integer which we'll call N, specifies the total number of vectors that will be read in. If N is not a positive integer, the program should immediately terminate (without reading another integer). The second integer which we'll call D, specifies the dimensionality of each vector (i.e. how many numbers each vector stores). If D is not a positive integer, the program should terminate.
You will need to store the vectors in the .data segment of your program as they are read in. Your program should support a total of thousand words of vector data (4 bytes per dimension of each vector). So this could store 100 vectors with 10-dimensions, or 200 vectors with 5-dimensions, etc. If the first two integers would imply an input size greater than one thousand, your program should immediately terminate without reading any further data.
Part 2: Reading vectors.
In the next phase, your program needs to read in the vector data into the .data segment. Your program can expect to receive exactly the required number of integers to populate the N vectors where each vector has D dimensions. Each element of a vector is an integer word.
How you layout your memory is up to you. But a sensible approach would be to have a label at the start of your data segment. After this label you would store the D words that represents the first vector's data. Immediately after this would appear the D words for the second vector, and so on.
Part 3: Multiplying vectors.
In the final phase, the program first reads an integer specifying the 0-based index of a vector. If this first index read in is out of range, the program should terminate. The program then reads in a second index. If the second index read in is out of range, the program should terminate. Note that both indexes could be the same.
If both indexes are valid, the program then prints out the element-wise multiplication of the two vectors. That is, you should multiply together the first element of both vectors, the second element of both vectors, etc. The output should be D integers. The integers are separated by a space. After the D integers, the program should print a line feed.
You can assume that no multiplication will result in a number greater than a 32-bit signed integer can handle (i.e. we won't be testing your program with vectors with large values).
Here are some example runs. Lines starting with # are comments we added to explain each example, they should not be input or output by your program.
### Test 1: number of vectors not valid
0
-- program is finished running --
### Test 2: number of dimensions not valid
2
-1
-- program is finished running --
### Test 3: exceeded maximum of thousand words
101
10
-- program is finished running --
### Test 4: two vectors with positive values
### vector 0: (1, 2, 3)
### vector 1: (4, 5, 6)
2
3
1
2
3
4
5
6
0
1
4 10 18
1
0
4 10 18
0
0
1 4 9
1
1
16 25 36
-1
-- program is finished running --
### Test 5: three vectors with positive and negative values
### vector 0: (-5, 11, 13, -33)
### vector 1: (0, -1, 0, 1)
### vector 2: (8, 12, 99, -40)
3
4
-5
11
13
-33
0
-1
0
1
8
12
99
-40
0
1
0 -11 0 -33
0
2
-40 132 1287 1320
1
2
0 -12 0 -40
2
2
64 144 9801 1600
2
3
-- program is finished running --
How do I print a line feed?
li $v0, 11 # syscall 11 prints character in $a0
addi $a0, $0, 10 # ASCII code 10 is a line feed
syscall
How do I print a space?
li $v0, 11 # syscall 11 prints character in $a0
addi $a0, $0, 32 # ASCII code 32 is a space
syscall
the program have to print out exactly like the example output.
This is the code I have but it's not printing correctly
.data
vectors: .space 1000 #reserve space for up to 100 vectors of 10 dimensions
newline:.asciiz "\n"
.text
main:
#part 1: Initial Input
li$v0, 5
syscall
move$t0, $v0#$t0 = N
#check if N is a positive integer
bltz$t0, terminate
li$v0,5
syscall
move$t1, $v0#t1 = D
#check if D is a positive integer
bltz$t1, terminate
#check if the total size of the input vectors does not exceed 1000 words
mul$t2, $t0, $t1#$t2 = N*D
li$t3, 1000
bgt$t2, $t3, terminate
#Part 2: reading vectors
la$a0, vectors#load address of vextors
move$a1, $t1
move$a2, $t0
jalread_vectors#jump to read_vectors
#Part 3: Multiplying vectors
li$v0, 4
la$a0, vectors
jalprint_vectors #jump to print_vectors
#terminate the program
terminate:
li$v0,10#syscall 10 exits the program
syscall
read_vectors:
move$t4, $a0
move$t5, $a1
move$t6, $a2
read_loop:
bnez$t6, read_vectors
jr$ra
read_vector:
read_element_loop:
beqz$t5, next_vector
li$v0, 5
syscall
sw$v0, 0($t4)
addi$t4, $t4, 4
addi$t5, $t5, -1
jread_element_loop
next_vector:
addi$t6, $t6, -1
jread_loop
print_vectors:
move$t7, $a0
move$t8, $a1
print_loop:
bnez$t8, next_line
lw$t9, 0($t7)
addi$t7, $t7, 4
li$v0, 1
move$a0, $t9
syscall
#printing space
li$v0, 11
addi$a0, $0, 32
syscall
addi$t8, $t8, -1#decrement the remaining dimensions
jprint_loop
next_line:
#print newline
li$v0, 4
la$a0, newline
syscall
li$v0, 10
syscall
Cost Management A Strategic Emphasis
ISBN: 978-0078025532
6th edition
Authors: Edward Blocher, David Stout, Paul Juras, Gary Cokins