This program is supposed to be created using assembly language on MARS. It ask t
ID: 3672880 • Letter: T
Question
This program is supposed to be created using assembly language on MARS. It ask the user to create 2 arrays that take user input of integers ( the numbers should be able to be either whole positive or whole negative numbers). The sum is computed for each of the arrays and displayed out to the console. The average from each array should also be displayed out to the console. Finally a dot product should be created from the two arrays made and displayed out the console. Could you also comment on each step that is changed so I know exactly what each part is doing? Thanks!
I can get it to read the two arrays, but the sums, means, and dot product are not being computed properly....
.data
size: .word 10
array1: .word 10
array2: .word 10
N: .float .1
hundred: .float 100.00
msg1: .asciiz "Please enter 10 values for array A"
msg2: .asciiz "Please enter 10 values for array B"
msg3: .asciiz " Mean(average) if array A: "
msg4: .asciiz " Mean(average) of array B: "
msg5: .asciiz " Dot product of the two arrays is: "
.text
.globl main
main:
li $s1,10 # Value to exit counters, N times
la $t0,array1 # array A in $s3
la $t4,array2 # array B in $s4
l.s $f10,N # Store N as .1 (1/10) where N = 10
l.s $f11,hundred # Used for rounder
li $s2,0 # Initialize counter at 0 for array
li $v0,4 # Array A input message
la $a0,msg1
syscall
loopA:
beq $s2,$s1,next # Branch at 10 iterations
li $v0,6 # Loop to input
syscall
s.s $f0,0($t0) # Store in $s3
l.s $f1,0($t0)
addi $s2,$s2,1 # Increment counter
addi $t0,$t0,4 # Next array position
j loopA
next:
li $s2,0 # Reset counter to 0
li $v0,4 # Message array B
la $a0,msg2
syscall
loopB:
beq $s2,$s1,next2 # Branch at 10 iterations
li $v0,6 # Loop to input
syscall
s.s $f0,0($t4) # Store in $s4
addi $s2,$s2,1 # Increment counter
addi $t4,$t4,4 # Next array position
j loopB
next2:
li $s2,0 # Reset counter
addi $t0,$t0,-40 # Reset Array A back to start
addi $t4,$t4,-40 # Reset Array B back to start
loopC:
beq $s2,$s1,exit # Branch at 10 iterations
mov.s $f12,$f1 # testing output value
li $v0,2
syscall
l.s $f1,0($t0) # Load value stored in array A
mov.s $f12,$f1 # testing output value
li $v0,2
syscall
l.s $f2,0($t4) # Load value stored in array B
add.s $f3,$f3,$f1 # Summation of A
add.s $f4,$f4,$f2 # Summation of B
mul.s $f29,$f1,$f2 # A * B
add.s $f20,$f20,$f29 # (A*B)/>/> + total
addi $s2,$s2,1 # Increment counter
addi $t0,$t0,4 # Next position array A
addi $t4,$t4,4 # Next position array B
j loopC
exit:
mul.s $f3,$f3,$f10 # Divide Summation A by 10
mul.s $f4,$f4,$f10 # Divide Summation B by 10
mov.s $f12,$f3 # Move Mean of A to $f12
la $a0,msg3 # Mean A message
li $v0,4
syscall
li $v0,2 # Print Mean value A
syscall
mov.s $f12,$f4 # Move Mean of B to $f12
la $a0,msg4 # Mean B message
li $v0,4
syscall
li $v0,2 # Print Mean value B
syscall
mov.s $f12,$f20 # Move Dot Product to $f12
la $a0,msg5 # Dot Product message
li $v0,4
syscall
li $v0,2 # Print Dot Product value
syscall
li $v0,10 # Exit Program
syscall
Explanation / Answer
Below code is working fine. Please check.
.data
size: .word 10
array1: .word 10
array2: .word 10
N: .float .1
hundred: .float 100.00
msg1: .asciiz "Please enter 10 values for array A"
msg2: .asciiz "Please enter 10 values for array B"
msg3: .asciiz " Mean(average) if array A: "
msg4: .asciiz " Mean(average) of array B: "
msg5: .asciiz " Dot product of the two arrays is: "
.text
.globl main
main:
li $s1,10 # Value to exit counters, N times
la $t0,array1 # array A in $s3
la $t4,array2 # array B in $s4
l.s $f10,N # Store N as .1 (1/10) where N = 10
l.s $f11,hundred # Used for rounder
li $s2,0 # Initialize counter at 0 for array
li $v0,4 # Array A input message
la $a0,msg1
syscall
loopA:
beq $s2,$s1,next # Branch at 10 iterations
li $v0,6 # Loop to input
syscall
s.s $f0,0($t0) # Store in $s3
l.s $f1,0($t0)
addi $s2,$s2,1 # Increment counter
addi $t0,$t0,4 # Next array position
j loopA
next:
li $s2,0 # Reset counter to 0
li $v0,4 # Message array B
la $a0,msg2
syscall
loopB:
beq $s2,$s1,next2 # Branch at 10 iterations
li $v0,6 # Loop to input
syscall
s.s $f0,0($t4) # Store in $s4
addi $s2,$s2,1 # Increment counter
addi $t4,$t4,4 # Next array position
j loopB
next2:
li $s2,0 # Reset counter
addi $t0,$t0,-40 # Reset Array A back to start
addi $t4,$t4,-40 # Reset Array B back to start
loopC:
beq $s2,$s1,exit # Branch at 10 iterations
mov.s $f12,$f1 # testing output value
li $v0,2
syscall
l.s $f1,0($t0) # Load value stored in array A
mov.s $f12,$f1 # testing output value
li $v0,2
syscall
l.s $f2,0($t4) # Load value stored in array B
add.s $f3,$f3,$f1 # Summation of A
add.s $f4,$f4,$f2 # Summation of B
mul.s $f29,$f1,$f2 # A * B
add.s $f20,$f20,$f29 # (A*B)/>/> + total
addi $s2,$s2,1 # Increment counter
addi $t0,$t0,4 # Next position array A
addi $t4,$t4,4 # Next position array B
j loopC
exit:
mul.s $f3,$f3,$f10 # Divide Summation A by 10
mul.s $f4,$f4,$f10 # Divide Summation B by 10
mov.s $f12,$f3 # Move Mean of A to $f12
la $a0,msg3 # Mean A message
li $v0,4
syscall
li $v0,2 # Print Mean value A
syscall
mov.s $f12,$f4 # Move Mean of B to $f12
la $a0,msg4 # Mean B message
li $v0,4
syscall
li $v0,2 # Print Mean value B
syscall
mov.s $f12,$f20 # Move Dot Product to $f12
la $a0,msg5 # Dot Product message
li $v0,4
syscall
li $v0,2 # Print Dot Product value
syscall
li $v0,10 # Exit Program
syscall
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.