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

Write, and test using SPIM, an assembly language program that accepts as input a

ID: 3920932 • Letter: W

Question

Write, and test using SPIM, an assembly language program that accepts as input a sequence of 10 integers. These numbers should be stored in an array in memory, in the order in which they were input. Next, your program should prompt the user for two integers i and j. If both i and j are between 1 and 10, your program should swap the i’th and j’th integers in the array, and then prompt the user for another two integers i and j, and repeat. When one or both of i and j is outside the range of 1 to 10, your program should output the elements of the array according to their current ordering, and then terminate.

Explanation / Answer

ScreenShot

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

Code

#Variable declaration
.data
     #Space allocated for array
     array: .space 40
     #Enter array prompt
     inputPrompt: .asciiz "Enter element into array: "
     #Array display
     outputPrompt: .asciiz "Array is: "
     #Ask indexes
     ith: .asciiz "Enter i index: "
     jth: .asciiz "Enter j index: "
     #For space between array values display
     space: .asciiz " "
     #To goto next line
     newLine: .asciiz " "
#Main program
.text
.globl main
main:
     #Variable for looping
     addi $t0,$0,0
     #Address of he array
     la $s0,array
#Loop to read array values
loop:
     beq $t0,10,next
     #Prompt
     la $a0,inputPrompt
     li $v0,4
     syscall
     #Read
     li $v0,5
     syscall
     #Store into array
     sb $v0,0($s0)
     addi $s0,$s0,1
     addi $t0,$t0,1
     j loop
#Prompt and read index values for swap
next:
     la $a0,ith
     li $v0,4
     syscall
     li $v0,5
     syscall
     #Check greater than 10 or less than 1
     bgt $v0,10,ArrayDisplay
     blt $v0,1,ArrayDisplay
     move $t1,$v0
     la $a0,jth
     li $v0,4
     syscall
     li $v0,5
     syscall
     #Check greater than 10 or less than 1
     bgt $v0,10,ArrayDisplay
     blt $v0,1,ArrayDisplay
     move $t2,$v0
     addi $t0,$0,0
     la $s0,array
     #get ith value
check:
     bgt $t0,$t1,findIndex
     lb $s1,0($s0)
     addi $s0,$s0,1
     addi $t0,$t0,1
     j check
     #get jth value
findIndex:
    move $t3,$s1
    addi $t0,$0,0
    la $s0,array
nextCheck:
     bgt $t0,$t2,findIndex2
     lb $s1,0($s0)
     addi $s0,$s0,1
     addi $t0,$t0,1
     j nextCheck
     #swap jth val with ith val
findIndex2:
    move $t4,$s1
    addi $s0,$s0,-1
    sb $t3,($s0)
    addi $t0,$0,0
    la $s0,array
    #To get ith position
sNext:
    bgt $t0,$t1,swap
    lb $s1,0($s0)
     addi $s0,$s0,1
     addi $t0,$t0,1
     j sNext
     #swap jth val with ith
swap:
    addi $s0,$s0,-1
    sb $t4,($s0)
    j next
#if entered i and j not between 1-10 then display array
ArrayDisplay:
     addi $t0,$0,0
     la $s0,array
     la $a0,outputPrompt
     li $v0,4
     syscall
#Display array values
loopNext:
     beq $t0,10,exit
     lb $a0,0($s0)
     li $v0,1
     syscall
     la $a0,space
     li $v0,4
     syscall
     addi $s0,$s0,1
     addi $t0,$t0,1
     j loopNext
#End of the program
exit:
    la $a0,newLine
     li $v0,4
     syscall
     li $v0,10
     syscall

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

Output

Enter element into array: 1
Enter element into array: 2
Enter element into array: 3
Enter element into array: 4
Enter element into array: 5
Enter element into array: 6
Enter element into array: 7
Enter element into array: 8
Enter element into array: 9
Enter element into array: 10
Enter i index: 2
Enter j index: 4
Enter i index: 1
Enter j index: 3
Enter i index: 11
Array is: 1 4 5 2 3 6 7 8 9 10

-- program is finished running --