Question

In: Computer Science

Written in MIPS assembly language If given an array which is sorted in ascending order, as...

Written in MIPS assembly language

If given an array which is sorted in ascending order, as well as its length and a target value. You are asked to find the index of this target value in the array using binary search algorithm. Here is an example: array 1, 4, 6, 8, 10, 12 length 6 target 8 2 In this case, the returned result should be assigned to be 3, as array[3] is equal to target. (Note: The target will exist in this array and will not be the first or last element, so there is no need to consider corner cases.) This task should be easy for high-level programming languages, such as C++, Java or Python. However, in this lab, you are asked to implement it in assembly language. Writing programs in a low-level language helps you learn how high-level operations are handled by CPUs.. The size of the sorted array can be fairly large, up to 2048 elements. Please avoid storing temporary variables in data memory (store them in registers instead). In order to simply your future work you the operators and operands that you are allowed to used in your code are limited to those given below: lui, lw, sw, add, sub, addi, srl, sll, and, or, andi, ori, xor, nor, slt, beq, bne, j, t0-t7, s0-s7, zero. Use the template "lab1-template.s" on git and fill the blank starting from "main:" with your code. (Please DO NOT change anything else.) SPIM automatically makes the program counter pc point to the main function when initialized. Your answer should be stored in the address of result, which is 0x10010008 in the template. Check you code with the provided correct pairs of input and output before submission.

Solutions

Expert Solution

# load $s0: base address
# load $s1: array size
# $s2: the search element
# return index at $v0 at address 0x10010008

.data
myArray:   .word 1, 4, 6, 8, 10, 12
arraySize: .word 6

.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 0
    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

Related Solutions

What is the running time of Quicksort when the input array is sorted in ascending order?...
What is the running time of Quicksort when the input array is sorted in ascending order? (please explain in detail)
This is to be done with MIPS assembly language. Write MIPS code which is equivalent to...
This is to be done with MIPS assembly language. Write MIPS code which is equivalent to the follow java program: int day = (int)(Math.random() * 7); switch (day) { case 1: System.out.println(“Monday”); break case 2: System.out.println(“Tuesday”); break case 3: System.out.println(“Wednesday”); break case 4: System.out.println(“Thursday”); break case 5: System.out.println(“Friday”); break case 6: System.out.println(“Saturday”); break case 0: System.out.println(“Sunday”); break }
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...
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...
MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user toinput an...
MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user to input an integer and then prints out a string that shows how that integer should be encoded using 16 bits. Your program should handle both positive and negative valued inputs. Your program should also print out an error message if the given input cannot be expressed as a 16 bit signed integer.As an example, if the input is 12, your program should output “0000000000001100”. If the input...
Thinking in Assembly language What values will be written to the array when the following code...
Thinking in Assembly language What values will be written to the array when the following code executes? .data array DWORD 4 DUP(0) .code main PROC mov eax,10 mov esi,0 call proc_1 add esi,4 add eax,10 mov array[esi],eax INVOKE ExitProcess,0 main ENDP proc_1 PROC call proc_2 add esi,4 add eax,10 mov array[esi],eax ret proc_1 ENDP proc_2 PROC call proc_3 add esi,4 add eax,10 mov array[esi],eax ret proc_2 ENDP proc_3 PROC mov array[esi],eax ret proc_3 ENDP
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the...
Write a MIPS assembly language procedure that implements the Towers of Hanoi recursive function given the following declaration: void towers(int n, char source, char dest, char spare); The function outputs a message describing each move. The source, destination, and spare poles are indicated with a character identifier of your choosing ('A', 'B', 'C' are common). Write a MIPS assembly language program that demonstrates the Towers of Hanoi procedure. Your program should ask the user for the number of disks. The...
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i)...
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i) the range (max - min) and (ii) the interquartile range (IQR) of the numbers. IQR is defined as the difference between the number at the 25% percentile and the number at the 75% percentile. What is the exact number of comparisons needed for computing the range and why? Using any algorithm from the chapters of the textbook covered so far as a subroutine, give...
Assembly Language. Write a procedure that reverses order of a 1-D array using the stack. The...
Assembly Language. Write a procedure that reverses order of a 1-D array using the stack. The array is a string array, and the address along with the number of elements are passed through the stack. Then write a complete program to test your procedure.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT