In MIPS, I have this code and I don't know how to make it so that there are to arrays whose sizes are Controled by a vector. Like-- "Please enter the size of the vector." and you choose five and so the arrays sizes are five. "Enter in the numbers for Array 1". You enter 10 10 10 10 10
. Then it asks you to enter in the size for array 2. lastly it shows the the sum and averages for both array 1 and array 2. Heres what I have so far.
.data
prompt: .asciiz "Enter 10 Elements: \n"
avg: .asciiz "Average of these numbers is: "
sum: .float 0
count: .float 10
.text
main:
#size of the vector using prompt
la $a0 prompt
li $v0 4
syscall
li $t1, 40 # t1 has count
li $t2, 0 # t2 is iterator
l.s $f12, sum # t3 has sum
while: bge $t2, $t1, end
li $v0, 6
syscall
add.s $f12, $f12, $f0
addi $t2, $t2, 4
b while
end:
la $a0 avg
li $v0, 4
syscall
lwc1 $f10, count
div.s $f12, $f12, $f10
li $v0, 2
syscall
li $v0, 10
syscall
li $v0, 2
lwc1, $f12 sum
It took me a little while to refresh my memory of MIPS. I wrote up a somewhat small program that asks the user for the size of the vector. Using that size it then dynamically allocates two integer arrays and prompts the user to fill said arrays. Once the arrays have been filled, via user input, the program then prints the first array followed by its sum, then the second array followed by its sum.
I noticed that your code uses floating point numbers. Since I don't know if this is a homework assignment or school project, I thought I'd let you figure out how to convert the below code to use floating point numbers. Also, I purposely left out the code to calculate the average for the same reason (but I did leave comments where the code could possibly be added). Modifying the program to use floating point numbers and adding an average calculation shouldn't be to hard. Let me know if you need further assistance in the comments section of this answer.
Sorry about the spacing of some of the comments in the code, copying from the program I used didn't go so well.
Vector Program
# ====== DATA SEGMENT ====== #
.data
prompt: .asciiz "Enter Size of the Vector:\n"
enter: .asciiz "Enter the "
first: .asciiz " numbers in the first array:\n"
second: .asciiz " numbers in the second array:\n"
sum: .asciiz "Sum of these numbers is: "
avg: .asciiz "Average of these numbers is: "
open: .asciiz "[ "
close: .asciiz " ]\n"
comma: .asciiz ", "
lf: .asciiz "\n"
a1: .word 0 # first array
a2: .word 0 # second array
# syscall variables
printi: .word 1 # print_int
printf: .word 2 # print_float
prints: .word 4 # print_string
readi: .word 5 # read_int
readf: .word 6 # read_float
sbrk: .word 9
exit: .word 10
# ====== TEXT SEGMENT ====== #
.text
main:
# initialize and describe registers
li $s0, 0 # vector size
li $s1, 0 # first array's sum
li $s2, 0 # second array's sum
li $s6, 0 # temporary sum store
li $s7, 0 # temporary array address store
li $t9, 0 # loop iterator
# prompt for vector size
la $a0, prompt
lw $v0, prints
syscall
# get vector size integer into $s0
lw $v0, readi
syscall
add $s0, $zero, $v0 # store vector size in $s0
# dynamically allocate first array
mul $t1, $s0, 4 # put size * 4 in $t1
add $a0, $zero, $t1 # put mul result in $a0
lw $v0, sbrk # allocate size * 4 bytes in memory
syscall
sw $v0, a1 # put address returned by sbrk in a1
# dynamically allocate second array
lw $v0, sbrk # allocate another size * 4 bytes in memory
syscall
sw $v0, a2 # put address returned by sbrk in a2
# prompt for first array elements
jal print_enter
la $a0, first
lw $v0, prints
syscall
# fill the first array and calculate sum
lw $s7, a1
jal array_fill
add $s1, $zero, $s6 # put first sum in $s1
# prompt for second array elements
jal print_enter
la $a0, second
lw $v0, prints
syscall
# fill the second array and calculate sum
lw $s7, a2
jal array_fill
add $s2, $zero, $s6 # put second sum in $s2
# print the first array
lw $s7, a1
jal print_array
# print the sum of the first array
la $s6, ($s1)
jal print_sum
# TODO: Add array1 average calculation and output
# print the second array
lw $s7, a2
jal print_array
# print the sum of the second array
la $s6, ($s2)
jal print_sum
# TODO: Add array2 average calculation and output
b end
print_enter:
# print "Enter the "
la $a0, enter
lw $v0, prints
syscall
# print the number of elements to enter
add $a0, $zero, $s0
lw $v0, printi
syscall
jr $ra
array_fill:
la $s6, ($zero) # set tmp sum to 0
la $t9, ($zero) # set iterator to 0
array_fill_for: bge $t9, $s0, end_array_fill
# get integer from user
lw $v0, readi
syscall
sw $v0, ($s7) # store integer at current array index
add $s6, $s6, $v0 # keep a running sum in $s6
add $s7, $s7, 4 # get address of next array element
add $t9, $t9, 1 # increment iterator
b array_fill_for
end_array_fill:
jr $ra
print_array:
# print opening bracket of array
la $a0, open
lw $v0, prints
syscall
la $t9, ($zero) # set iterator to 0
add $t0, $s0, -1 # loop over size-1 elements
print_array_for: bge $t9, $t0, end_print_array
# print array element at current index
lw $a0, ($s7)
lw $v0, printi
syscall
# print a seperating comma
la $a0, comma
lw $v0, prints
syscall
add $s7, $s7, 4 # get address of next array element
add $t9, $t9, 1 # increment iterator
b print_array_for
end_print_array:
# print last index of array without comma
lw $a0, ($s7)
lw $v0, printi
syscall
# print closing bracket of array
la $a0, close
lw $v0, prints
syscall
jr $ra
print_sum:
# print the sum greeting
la $a0, sum
lw $v0, prints
syscall
# print the sum that's in $s6
la $a0, ($s6)
lw $v0, printi
syscall
# print a line feed
la $a0, lf
lw $v0, prints
syscall
jr $ra
end:
lw $v0, exit
syscall
Test Input/Output
Enter Size of the Vector:
4
Enter the 4 numbers in the first array:
1
2
3
4
Enter the 4 numbers in the second array:
5
6
7
8
[ 1, 2, 3, 4 ]
Sum of these numbers is: 10
[ 5, 6, 7, 8 ]
Sum of these numbers is: 26
-- program is finished running --
Some Links I Found Helpful
- MIPS Instruction
Reference
- MIPS Dynamic Memory Allocation using
sbrk