Question

Linear Search: Write a MIPS assembly language program that can search for a number that entered...

Linear Search: Write a MIPS assembly language program that can search for a number that entered by user in an array with 20 integer numbers and prints the index of the number in the array if it is found and -1 if not found

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE

Text Code

#Data Segment
.data

prompt: .asciiz "Enter an integer value : "
matched_output: .asciiz "Found Index Is : "
unmatched_output: .asciiz "Not Found : "
#Define the array
list: .word 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 32, 45, 9, 0, 234, 12, 42, 65, 33, 67  
#Define the size of array
size: .word 20

#Text Segment
.text

#Main Function Code starts Here
main:

#Give message to user
li $v0, 4 # system call code for printing a string = 4
la $a0, prompt # address of string is argument 0 to
syscall # call operating system to perform operation defined in register v0

#Input From USer
li $v0, 5 # get ready to read in integers code is 5
syscall # call operating system to perform operation defined in register v0
# NOTE VALUE IS PRESENT IN value in $v0
move $s0, $v0 #Now register $s0 contains the number entered by user

la $t1, list # get array address
li $t2, 0 # set loop counter as 0
la $t4, size #get array size address
lw $t3, ($t4) #get size in register $t3

#start the loop
loop:
beq $t2, $t3, loop_end # checking for array end comparing $t2 and t3 if reaches end then call loop_end
lw $t5, ($t1) # value at the array pointer in register t5
beq $t5, $s0, matched #matching array value with user value if it matches matched functino is called else continue

addi $t2, $t2, 1 # increment loop counter
addi $t1, $t1, 4 # increment array pointer
j loop # repeat the loop

#This function will be called when we reach the end of array
#therefore its evident that number is not Found
loop_end:

#Print message First
li $v0, 4 # system call code for printing string = 4
la $a0, unmatched_output # load address of string to be printed into $a0
syscall # call operating system to perform operation defined in register v0
#Print -1
li $v0, 1 # system call code for printing int = 1
la $a0, -1 # load integer in a0 register
syscall # call operating system to perform operation defined in register v0
#terminate the program
li $v0, 10 # terminate program code is 10
syscall # call operating system to perform operation defined in register v0

#this function is called when we find a match in the array
matched:

#Print message First
li $v0, 4 # system call code for printing string = 4
la $a0, matched_output # load address of string to be printed into $a0
syscall # call operating system to perform operation defined in register v0
#Print Index which was in register $t2
li $v0, 1 # system call code for printing int = 1
la $a0, ($t2) # load value in $t2
syscall # call operating system to perform operation defined in register v0

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

INput Data defined in data segment

Sample Output

Please comment for any further assistance

Add a comment
Know the answer?
Add Answer to:
Linear Search: Write a MIPS assembly language program that can search for a number that entered...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT