Write the program in MIPS that declares an array of positive
Write the program in MIPS that declares an array of positive integers. The size of the array is not fixed. The final element of the array should have the value of 0xF, which is not used in calculations, and should be used to calculate the size of the array. The program can prompt the user for an integer input. Compare the input with the array. Display the index of the input in the array if found. If the number does not match any element in the array, insert the number in the array in sorted order. The program is terminated when input a negative integer. Print the new array with appropriate message on the screen. For example assume an array as follows:
(array: .word 4, 5, 23, 5, 8, 3, 15, 67, 8, 9, 0xF)
Sorted array: 3, 4, 5, 5, 8, 8, 9, 15, 23, 67
Enter a number to search: 20
Number not found. Added to array.
Sorted array: 3, 4, 5, 5, 8, 8, 9, 15, 20, 23, 67
Enter a number to search: 5
Number at index 2
Enter a number to search: -1 Program terminated.
Solution
.data
array: .space 100
input: .asciiz \"Enter at least 4 integers: Enter the number oxF to exit \ \"
output: .asciiz \"The array in ascending order: \ \"
commas: .asciiz \",\"
newline .asciiz\"\ \"
.text
.globl main
main:
la $s1, array #loads a pointer to array into $s1
li $s2,9 #loads 9 into $s2
li $t0,0
li $t1,oXF
loops:
la $s0, input #loads input text into $a
li $v0, 4 #loads 4 into $v0 (prints string)
syscall
li $v0, 5 #loads 5 into $v0 (read interger)
syscall
beq $v0,$t1,swap
addi $t0,$t0,4 #add 4 to $t0, save to $t0
sw $v0, ($s1) #stores input into array
addi $s1, $s1,4 #add 4 to $s1, save to $s1
j loops
swap:
la $t4, array #loads array to $t4
la $t1, array #loads array to $t1
addi $t1,$t1,4 #add 4 to $t1, save to $t1
la $t8,array #loads array to $t8
add $t8,$t0,$t8 #add $t8 to $t0, save to $t8
la $t9,array
add $t9,$t0,$t9 #add $t9 to $t0, save to $t9
addi $t9,$t9,-4 #subtracts 4 from $t9, save to $t9
loop:
lw $t2,($t4) #load input into $t2
lw $t3,($t1) #load input into $t3
blt $t2,$t3,loop1 #if $t2 < $t3, go to loops
sw $t3,($t4) #store $t3 in $t4
sw $t2,($t1) #store $t2 in $t1
loop1:
addi $t1,$t1,4 #add 4 to $t1, save to $t1
blt $t1,$t8,loop #if $t1<$t8, go to loop
addi $t4,$t4,4 #add 4 to $t4, save to $t4
move $t1,$t4
addi $t1,$t1,4 #add 4 to $t1, save to $t1
blt $t4,$t9,loop #if $t4<$t9, to go loop
print:
la $s1,array #loads array to $s1
la $s0, output #loads output to $s0
li $v0, 4 #loads 4 into #v0
syscall
loop2:
blez $t0, done #if $t0<=0, go to done
li $v0, 1 #loads 1 into $v0
add $t1, $s1, $t0
addi $t1, $t1, -4
lw $s0, 0($t1) #load an inout into $s0
syscall
la $s0, commas #loads commas into $s0
li $v0, 4 #loads 4 into $v0
syscall
addi $s1, $s1, 4 #add 4 to $s1, save to $s1
addi $t0, $t0, -4 #subtracts 4 from #t0, save to $t0
j loop2
done:
j done

