Question

In: Computer Science

Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers.


Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers. Your program should have a main routine that does the following:

(a) Prompt the user to enter all the 10 integers in the array.

(b) Prompt the user to enter the number to be searched.

(c) Reads the integer values and makes sure it is a positive integer.

(d) Prints the index of the integer. If the input is not available in the string, the code should print a string: "Element not present in the array".

Please add comments next to each MIPS Instruction stating what it is exactly doing. Also include the psuedo code in C for your MIPS program.

Solutions

Expert Solution

  • # load $s0: base address
  • # load $s1: array size
  • # $s2: the search element
  • # return index at $v0
  • .data
  • myArray: .word 1, 4, 5, 7, 9, 12, 15, 17, 20, 21, 30
  • arraySize: .word 11
  • .text
  • .globl main
  • main:
  • # perform the first test
  • addi $a1, $zero, 15
  • jal binarySearch
  • add $s3, $v0, $zero
  • exit:
  • li $v0, 10
  • syscall
  • binarySearch:
  • la $s0, myArray # load the base address to $s0
  • add $a0, $zero, $zero # $a0 is now the first index 0f
  • lw $s1, arraySize # load the array size to $s1
  • addi $s1, $s1, -1 # now $s1 is the last index
  • add $s2, $zero, $a1 # now store the search element into $s2
  • j loop # do the loop
  • loop:
  • add $t0, $s1, $a0 # have a temp reg to calculate the sum of high and low
  • sra $t1, $t0, 1 # store mid index value into $t1
  • add $t1, $t1, $s0 # move to the arr[middle]
  • lw $t1,($t1)
  • beq $t1, $s2, return # if the arr[mid] is equal to search value, return mid
  • slt $t2, $t1, $s2 # if mid < search, $t2 = 1
  • beq $t2, $zero, leftHalf # if mid > search, go left
  • j rightHalf # if mid < search, go right
  • leftHalf:
  • add $s1, $t1, -1 # modify the max, max=mid-1
  • j do_loop # back to the loop
  • rightHalf:
  • addi $t1, $t1, 1
  • add $a0, $zero, $t1 # modify the min, min=mid+1
  • j do_loop # back to the loop
  • return:
  • add $ra, $zero, $t1
  • jr $ra

Pseudo code for Binary search in C

   A ← sorted array
   n ← size of array
   x ← value to be searched

   Set lowerBound = 1
   Set upperBound = n 

   while x not found
      if upperBound < lowerBound 
         EXIT: x does not exists.
   
      set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
      
      if A[midPoint] < x
         set lowerBound = midPoint + 1
         
      if A[midPoint] > x
         set upperBound = midPoint - 1 

      if A[midPoint] = x 
         EXIT: x found at location midPoint
   end while
   
end procedure

Related Solutions

Use MIPS assembly language program to swap two of the integers in an integer array. The...
Use MIPS assembly language program to swap two of the integers in an integer array. The program should include the Swap function to swap the integers and the main function to call the Swap function. The main function should: • Pass the starting address of the array in $a0. • Pass the indices of the two elements to swap in $a1 and $a2. • Preserve (i.e. push onto the stack) any T registers that it uses. • Call the Swap...
// This program demonstrates a Binary Search, which search for a value // in an array,...
// This program demonstrates a Binary Search, which search for a value // in an array, assuming that the array is sorted in descending order. // You have to modify the function binarySearch() to search for a value // in an array that is sorted in ascending order. // NOTES: // Uncomment line 34 and comment line 32. You don't have to edit anything // else in the main(), just in the binarySearch() function. // EXAMPLES (using the array sorted...
Write a program in MIPS assembly language to perform the calculation of the following equation and...
Write a program in MIPS assembly language to perform the calculation of the following equation and save the result accordingly:    f = 5x + 3y + z Assumptions: - Registers can be used to represent variables x, y, z, and f - Initialize x, y, and z to values of your choice. f can be initialized to zero. - Use comments to specify your register usage and explain your logic
Write a mips assembly language program to ask the user to enter two integers A and...
Write a mips assembly language program to ask the user to enter two integers A and B and then display the result of computing the expression: A + 2B - 5.
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal....
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal. 1. Request input data type. 2. Request input data. 3. Request output data type. 4. Output the data. Use any algorithm
Using MIPS assembly language In this program, you should define an array of 10 elements in...
Using MIPS assembly language In this program, you should define an array of 10 elements in your data segment with these values: ? = {11, 12,−10, 13, 9, 12, 14, 15,−20, 0} a. Write a function which finds the maximum value of this array. b. Write another function which calculates the summation of this array. c. Call these functions in your main program, and print the outputs of these functions to the user i. “The maximum is 15” ii. “The...
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays...
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays the sum and the difference. In the .data section, define two variables num1 and num2 both words. Initialize num1 to 92413 10 and num2 to D4B 16 (use 0xD4B to initialize, Note that D4B is a hexadecimal number). Your main procedure/function should load the values of num1 and num2 into two temporary registers, and display them on the console window. Then add the values...
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Write a program in MIPS assembly language to convert an ASCII number string containing positive and...
Write a program in MIPS assembly language to convert an ASCII number string containing positive and negative integer decimal strings, to an integer. Your program should expect register $a0 to hold the address of a nullterminated string containing some combination of the digits 0 through 9. Your program should compute the integer value equivalent to this string of digits, then place the number in register $v0. If a non-digit character appears anywhere in the string, your program should stop with...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT