Search for a number in an array ask the user to enter a number and search for th
ID: 3917940 • Letter: S
Question
Search for a number in an array ask the user to enter a number and search for that number in the array tell if it is on the array or not The function has to accept a provided array in the main where it ask the users for the values to populate the array.This code has to be written in MIPS Search for a number in an array ask the user to enter a number and search for that number in the array tell if it is on the array or not The function has to accept a provided array in the main where it ask the users for the values to populate the array.
This code has to be written in MIPS The function has to accept a provided array in the main where it ask the users for the values to populate the array.
This code has to be written in MIPS
Explanation / Answer
ScreenShot
------------------------------------------------------
Program
#data declaration section
.data
#array space declaration
array: .space 100
#element asking prompt
element: .asciiz "Enter element in array(exit press -1): "
#Search element prompt
elementSearch: .asciiz "Enter element to search: "
#result print prompt
Result: .asciiz " found at index "
Result1: .asciiz " Not Found"
#Main Method
.text
#counter for array
li $s0,0
#load address of array
la $a0,array
#call array elements enter function
jal arrayFill
#prompt for read search value
la $a0,elementSearch
li $v0,4
syscall
#read search val
li $v0,5
syscall
#move into a1
move $a1,$v0
#load address of array
la $a0,array
#call search function
jal search
#end of the program
li $v0,10
syscall
#function to fill array
arrayFill:
#array base address
move $t0,$a0
#prompt array enter
la $a0,element
li $v0,4
syscall
#read user inpout and fill into array
li $v0,5
syscall
#read user inpout and fill into array loop
loop:
beq $v0,-1,retArrayFill
#counter increment
addi $s0,$s0,1
#store value into array
sb $v0,0($t0)
#prompt array enter
la $a0,element
li $v0,4
syscall
#read user inpout and fill into array
li $v0,5
syscall
#increment array index
addi $t0,$t0,1
#loop continue
j loop
#return from function
retArrayFill:
#return to main
jr $ra
#Function to search user entered elemnt
search:
#move array address into to
move $t0,$a0
#index register
li $t2,0
#Search loop
loopSearch:
#loop continue unti ened of the array
beq $s0,0,notFound
#each element in t1
lb $t1,0($t0)
#check each value
beq $t1,$a1,exit
#increment index
addi $t2,$t2,1
#Increment address
addi $t0,$t0,1
#decrement counter
addi $s0,$s0,-1
j loopSearch
#if elemnt found
exit:
#display search element
move $a0,$a1
li $v0,1
syscall
#display result found prompt
la $a0,Result
li $v0,4
syscall
#display index
move $a0,$t2
li $v0,1
syscall
#return from function
jr $ra
#If not found
notFound:
#display search element
move $a0,$a1
li $v0,1
syscall
#display result not found prompt
la $a0,Result1
li $v0,4
syscall
#return from function
jr $ra
------------------------------------------------------
Output
Enter element in array(exit press -1): 2
Enter element in array(exit press -1): 10
Enter element in array(exit press -1): 15
Enter element in array(exit press -1): 47
Enter element in array(exit press -1): 55
Enter element in array(exit press -1): 21
Enter element in array(exit press -1): -1
Enter element to search: 21
21 found at index 5
-- program is finished running --
Enter element in array(exit press -1): 25
Enter element in array(exit press -1): 14
Enter element in array(exit press -1): 12
Enter element in array(exit press -1): 11
Enter element in array(exit press -1): 18
Enter element in array(exit press -1): 20
Enter element in array(exit press -1): -1
Enter element to search: 1
1 Not Found
-- program is finished running --
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.