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

Write a MIPS Assembly Language program to perform the following operations: Request and read two integers...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers from the console (Dn and Up), Call a recursive function to compute the result int RecurseFunc( int Dn, int Up ) { if( Dn < 1 ) return 0; return Dn * Up + RecurseFunc( Dn - 1, Up + 1 ); } Print out the results
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers from the console (Dn and Up), Call a recursive function to compute the result int RecurseFunc( int Dn, int Up ) { if( Dn < 1 ) return 0; else return Dn * Up + RecurseFunc( Dn - 1, Up + 1 ); } Print out the results Submit your code and report here.
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers from the console (Dn and Up), Call a recursive function to compute the result int RecurseFunc( int Dn, int Up ) { if( Dn < 1 ) return 0; return Dn * Up + RecurseFunc( Dn - 1, Up + 1 ); } Print out the results
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 to solve the following problem: For a set of integers...
Write a MIPS assembly language program to solve the following problem: For a set of integers stored in an array, calculate the sum of the even numbers and the sum of the odd numbers. The program should store these numbers in memory variables: evenSum and oddSum. Numbers should be read from the array one at a time with a zero value (0) being used to signal the end of data in the array.
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
IN MIPS ASSEMBLY LANGUAGE Write an assembly program to read three 32-bit signed integers from the...
IN MIPS ASSEMBLY LANGUAGE Write an assembly program to read three 32-bit signed integers from the user. Determine the smallest of these three numbers and display this result. Don’t use loops. Prompt the user for each entered integer. Your output should look something like the following example. Enter an integer: 7556 Enter an integer: -22984 Enter an integer: 8875 -22984
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT