Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in mips : You must run the assembly program using the SPIM simulator. Please zip

ID: 3915471 • Letter: I

Question

in mips :

You must run the assembly program using the SPIM simulator. Please zip the following files:

The assembly programs ( using file extension .asm)

Problem 2. Write a function AVA(&X;, &Y;,&Z;, n,s, t) to perform the following absolute value vector addition, such that where &x;,&Y;, and &Z; refer to the array X, Y, Z's starting address, n is the size of the array, and s and t are integers. Please use the stack to pass these six parameters Also, write main programs that call the AVA function on the following data sets (note: please prepare two separate program files, one for each data set) .n 5, s-2, t-1, Y - (1, 2, 3, 4, 5), and Z -(-5,-4,-3,-2,-1) . n 10, s 3, t2,Y (-1, 3, -5, 7, -9, 2, -4, 6, -8, 10, and Z 1,2,3,4,5,6,7,8,9,10 For each data set, the main program should first print out Y and Z arrays, and then to calculate X and finally prints out X array.)

Explanation / Answer

ScreenShot

Problem2:-

n=5,s=2,t=1,y={1,2,3,4,5} and Z={-5,-4,-3,-2,-1}

#variable declaration
.data
#For x value store
X:   .word
#Y values
Y: .word 1 2 3 4 5
#Z values
Z: .word -5 -4 -3 -2 -1
#size of array
n: .word 5
#S value
s:.word 2
#t value
t: .word 1
#prompting values
xVector:.asciiz "Print X Vector values: "
yVector:.asciiz "Print Y Vector values: "
zVector:.asciiz "Print Z Vector values: "
nextline: .asciiz " "
space: .asciiz " "
#Main program
.text
.globl main
main:
#Stack initialization
addi $sp,$sp,-24
la $a0,X
sw $a0,0($sp)
la $a0,Y
sw $a0,4($sp)
la $a0,Z
sw $a0,8($sp)
la $a0,n
sw $a0,12($sp)
la $a0,s
sw $a0,16($sp)
la $a0,t
sw $a0,20($sp)
#print Y vector
#counter
addi $t0,$0,0
#n value in t1
lw $a0,12($sp)
lw $t1,($a0)
#display yVector prompt
la $a0,yVector
li $v0,4
syscall
#starting address of Y array
lw $t2,4($sp)
yPrint:
      #check size
      bge $t0,$t1,next
      #Display values
      lw $a0,0($t2)
      li $v0,1
      syscall
      #Put space
      la $a0,space
      li $v0,4
      syscall
      #increment counter
      addi $t0,$t0,1
      #increment array address
      addi $t2,$t2,4
      j yPrint
#Z values print
next:
     #next line
      la $a0,nextline
      li $v0,4
      syscall
      #zVector prompt
      la $a0,zVector
      li $v0,4
      syscall
      #address of z array
      lw $t2,8($sp)
      #counter
      addi $t0,$0,0
#print z values
zPrint:
      bge $t0,$t1,funCall
      lw $a0,0($t2)
      li $v0,1
      syscall
      la $a0,space
      li $v0,4
      syscall
      addi $t0,$t0,1
      addi $t2,$t2,4
      j zPrint
#call function
funCall:
     jal AVA
#Print x values
     #next line
      la $a0,nextline
      li $v0,4
      syscall
      #zVector prompt
      la $a0,xVector
      li $v0,4
      syscall
#address of x array
      lw $t2,0($sp)
      #counter
      addi $t0,$0,0
      #size of array
      lw $t1,12($sp)
      lw $t1,($t1)
#print z values
xPrint:
      bge $t0,$t1,exit
      lw $a0,0($t2)
      li $v0,1
      syscall
      la $a0,space
      li $v0,4
      syscall
      addi $t0,$t0,1
      addi $t2,$t2,4
      j xPrint
#End of the program
exit:
    #Uninitialize stack
    addi $sp,$sp,24
     li $v0,10
     syscall
#Function definition
AVA:
    #counter=n
    lw $t0,12($sp)
    lw $t0,($t0)
     #t1=s
    lw $t1,16($sp)
    lw $t1,($t1)
#t2=t
    lw $t2,20($sp)
    lw $t2,($t2)
    #starting address of x
    lw $t3,0($sp)
    #starting address of y
    lw $t4,4($sp)
    #starting address of z
    lw $t5,8($sp)
#X val calculation loop
loop:
   #size check
    beqz $t0,ret
    #check positive or negative
    lw $t6,0($t4)
    blt $t6,0,ypositive
back1:
    lw $t7,0($t5)
    blt $t7,0,zpositive
#x val finding
back2:
    #2*s
    mul $v0,$t1,2
   #2*s+t
    add $v0,$v0,$t2
    #y+2*s+t
    add $v0,$v0,$t6
    #y+z+2*s+t
    add $v0,$v0,$t7
    #x=v0
    sw $v0,0($t3)
    #Increment to get next values
    addi $t0,$t0,-1
    addi $t3,$t3,4
    addi $t4,$t4,4
    addi $t5,$t5,4
    j loop
    #convert y values positive
ypositive:
   mul $t6,$t6,-1
   j back1
   #convert z values positive
zpositive:
   mul $t7,$t7,-1
   j back2
#return to main
ret:
   jr $ra

Output

Print Y Vector values:
1 2 3 4 5
Print Z Vector values:
-5 -4 -3 -2 -1
Print X Vector values:
11 11 11 11 11
-- program is finished running --

-----------------------------------------------------------------------------------

n=10,s=3,t=2,y={-1 3 -5 7 -9 2 -4 6 -8 10} and Z={1,2,3,4,5,6,7,8,9,10}

#variable declaration
.data
#For x value store
X:   .word
#Y values
Y: .word -1 3 -5 7 -9 2 -4 6 -8 10
#Z values
Z: .word 1 2 3 4 5 6 7 8 9 10
#size of array
n: .word 10
#S value
s:.word 3
#t value
t: .word 2
#prompting values
xVector:.asciiz "Print X Vector values: "
yVector:.asciiz "Print Y Vector values: "
zVector:.asciiz "Print Z Vector values: "
nextline: .asciiz " "
space: .asciiz " "
#Main program
.text
.globl main
main:
#Stack initialization
addi $sp,$sp,-24
la $a0,X
sw $a0,0($sp)
la $a0,Y
sw $a0,4($sp)
la $a0,Z
sw $a0,8($sp)
la $a0,n
sw $a0,12($sp)
la $a0,s
sw $a0,16($sp)
la $a0,t
sw $a0,20($sp)
#print Y vector
#counter
addi $t0,$0,0
#n value in t1
lw $a0,12($sp)
lw $t1,($a0)
#display yVector prompt
la $a0,yVector
li $v0,4
syscall
#starting address of Y array
lw $t2,4($sp)
yPrint:
      #check size
      bge $t0,$t1,next
      #Display values
      lw $a0,0($t2)
      li $v0,1
      syscall
      #Put space
      la $a0,space
      li $v0,4
      syscall
      #increment counter
      addi $t0,$t0,1
      #increment array address
      addi $t2,$t2,4
      j yPrint
#Z values print
next:
     #next line
      la $a0,nextline
      li $v0,4
      syscall
      #zVector prompt
      la $a0,zVector
      li $v0,4
      syscall
      #address of z array
      lw $t2,8($sp)
      #counter
      addi $t0,$0,0
#print z values
zPrint:
      bge $t0,$t1,funCall
      lw $a0,0($t2)
      li $v0,1
      syscall
      la $a0,space
      li $v0,4
      syscall
      addi $t0,$t0,1
      addi $t2,$t2,4
      j zPrint
#call function
funCall:
     jal AVA
#Print x values
     #next line
      la $a0,nextline
      li $v0,4
      syscall
      #zVector prompt
      la $a0,xVector
      li $v0,4
      syscall
#address of x array
      lw $t2,0($sp)
      #counter
      addi $t0,$0,0
      #size of array
      lw $t1,12($sp)
      lw $t1,($t1)
#print z values
xPrint:
      bge $t0,$t1,exit
      lw $a0,0($t2)
      li $v0,1
      syscall
      la $a0,space
      li $v0,4
      syscall
      addi $t0,$t0,1
      addi $t2,$t2,4
      j xPrint
#End of the program
exit:
    #Uninitialize stack
    addi $sp,$sp,24
     li $v0,10
     syscall
#Function definition
AVA:
    #counter=n
    lw $t0,12($sp)
    lw $t0,($t0)
     #t1=s
    lw $t1,16($sp)
    lw $t1,($t1)
#t2=t
    lw $t2,20($sp)
    lw $t2,($t2)
    #starting address of x
    lw $t3,0($sp)
    #starting address of y
    lw $t4,4($sp)
    #starting address of z
    lw $t5,8($sp)
#X val calculation loop
loop:
   #size check
    beqz $t0,ret
    #check positive or negative
    lw $t6,0($t4)
    blt $t6,0,ypositive
back1:
    lw $t7,0($t5)
    blt $t7,0,zpositive
#x val finding
back2:
    #2*s
    mul $v0,$t1,2
   #2*s+t
    add $v0,$v0,$t2
    #y+2*s+t
    add $v0,$v0,$t6
    #y+z+2*s+t
    add $v0,$v0,$t7
    #x=v0
    sw $v0,0($t3)
    #Increment to get next values
    addi $t0,$t0,-1
    addi $t3,$t3,4
    addi $t4,$t4,4
    addi $t5,$t5,4
    j loop
    #convert y values positive
ypositive:
   mul $t6,$t6,-1
   j back1
   #convert z values positive
zpositive:
   mul $t7,$t7,-1
   j back2
#return to main
ret:
   jr $ra

Output

Print Y Vector values:
-1 3 -5 7 -9 2 -4 6 -8 10
Print Z Vector values:
1 2 3 4 5 6 7 8 9 10
Print X Vector values:
10 13 16 19 22 16 19 22 25 28
-- program is finished running --

-------------------------------------------------------------------

Note