Question

Write the following program in MIPS: a) declare an array A of the following numbers: 3,...

Write the following program in MIPS:

a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44

b) declare a variable called size which stores the number of element in array A, that is 10.

c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows:
search(array, size, number_To_Search)
e.g. search(A, 10, 12)
The subroutine should return 0 for false or 1 for true in register $v0.

d) The parameters to the search subroutine are stored in $a0=stores base address of A, $a1=stores the number of elements in A that is 10, $a2=stores the number to search for (e.g. 12)
e) In C++, you would write:
  for (int i=0; i<size; i++)
if A[i] == number_to_search return true;
return false

Your MIPS program is a translation of the C++ code.

f) You can test your program by writing the following instructions:
la $a0, A
lw $a1, size
addi $a2, $zero, 12
jal search   # search for 12 in the array A of size 10
lw $a0, $v0   # subroutine search result is store in $v0; its value is moved to $a0
lw $v0, 1    # code to print integer value in $a0
syscall
lw $v0, 10   # code to terminate program
syscall

Homework Answers

Answer #1

subroutine in MIPS to search an element is given below:

search(int*, int, int):

        daddiu  $sp,$sp,-48

        sd      $fp,40($sp)

        move    $fp,$sp

        sd      $4,16($fp)

        move    $3,$5

        move    $2,$6

        sll     $3,$3,0

        sw      $3,24($fp)

        sll     $2,$2,0

        sw      $2,28($fp)

        sw      $0,0($fp)

.L5:

        lw      $3,0($fp)

        lw      $2,24($fp)

        slt     $2,$3,$2

        beq     $2,$0,.L2

        nop

        lw      $2,0($fp)

        dsll    $2,$2,2

        ld      $3,16($fp)

        daddu   $2,$3,$2

        lw      $3,0($2)

        lw      $2,28($fp)

        bne     $2,$3,.L3

        nop

        li      $2,1                        # 0x1

        b       .L4

        nop

.L3:

        lw      $2,0($fp)

        addiu   $2,$2,1

        sw      $2,0($fp)

        b       .L5

        nop

.L2:

        move    $2,$0

.L4:

        move    $sp,$fp

        ld      $fp,40($sp)

        daddiu  $sp,$sp,48

        j       $31

        nop

.LC0:

        .word   3

        .word   5

        .word   8

        .word   10

        .word   12

        .word   2

        .word   76

        .word   43

        .word   90

        .word   44

MIPS for declaring array, variable size and calling subroutine search is given below:

ld      $2,%got_page(.LC0)($28)

        daddiu  $3,$2,%got_ofst(.LC0)

        ldl     $3,7($3)

        ldr     $3,%got_ofst(.LC0)($2)

        move    $6,$3

        daddiu  $3,$2,%got_ofst(.LC0)

        ldl     $4,15($3)

        ldr     $4,8($3)

        move    $5,$4

        daddiu  $3,$2,%got_ofst(.LC0)

        ldl     $4,23($3)

        ldr     $4,16($3)

        daddiu  $3,$2,%got_ofst(.LC0)

        ldl     $7,31($3)

        ldr     $7,24($3)

        move    $3,$7

        daddiu  $2,$2,%got_ofst(.LC0)

        ldl     $7,39($2)

        ldr     $7,32($2)

        move    $2,$7

        sd      $6,8($fp)

        sd      $5,16($fp)

        sd      $4,24($fp)

        sd      $3,32($fp)

        sd      $2,40($fp)

        li      $2,10                 # 0xa

        sw      $2,0($fp)

        lw      $3,0($fp)

        daddiu  $2,$fp,8

        li      $6,12                 # 0xc

        move    $5,$3

        move    $4,$2

        ld      $2,%got_disp(search(int*, int, int))($28)

        move    $25,$2

        nop

Thank You.

Know the answer?
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
Similar Questions
I'm trying to write a nested loop in Mips Assembly that prints out, for example, if...
I'm trying to write a nested loop in Mips Assembly that prints out, for example, if user input x was 5: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 (spaces in between) So far my code is : li $v0, 5   #Ask for integer syscall move $t5, $v0 addi $t0, $zero, 0 For1:    slt $t1, $t0, $t5    beq $t1, $zero, Exit    add $s0, $s0, $t0    addi $t0, $t0, 1...
The C++ program steps through the array x[]. For each i, if x[i] < x[i+1], i...
The C++ program steps through the array x[]. For each i, if x[i] < x[i+1], i is saved in the array ascend[], in order. Compile and run the program; it should print 0 4 5. In this exercise, you’ll try to translate the C++ program to MIPS. Some of the more tedious parts are already given in Assignment3.F19.s. You won’t have to write the data allocation sections, some of the initializations, and the output loop at the end. So the...
Write a C++ program to perform the following tasks     a) Declare an integer array of...
Write a C++ program to perform the following tasks     a) Declare an integer array of size 1000.     b) Initialize the array with random values between 1 and 9.     c) Write the code to find and print, how many 1’s occur in the array.
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...
Write a program which: Write a program which uses the following arrays: empID: An array of...
Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to hold each employee’s gross salary....
10. Number Array Class Design a class that has an array of floating-point numbers. The constructor...
10. Number Array Class Design a class that has an array of floating-point numbers. The constructor should accept an integer argument and dynamically allocate the array to hold that many numbers. The private data members of the class should include the integer argument in a variable to hold the size of the array and a pointer to float type to hold the address of the first element in the array. The destructor should free the memory held by the array....
Write a program to determine the minimum element in an array of ten elements. The program...
Write a program to determine the minimum element in an array of ten elements. The program should have the following: 1. Class Name as ArrayProcessing. The main method should create an array of size 10 2. There should be two methods besides the main method in the class namely inputArray and MinimumElement 3. InputArray method should assign the ten elements in the array. Use scanner to input these elements. The array of 10 numbers in the method "InputArray" should be...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array,...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array, and an integer number. It should return the position where the integer value is found. In case the number does not exist in that array it should return the index where it should have been if it were present in this sorted array. Use pointer notation of arrays for this question. c++ code
USE PYTHON LANGUAGE PLEASE FOCUS YOU SHOULD ENTER AN ARRAY AND THEN THE PROGRAM GIVE OUTPUT(...
USE PYTHON LANGUAGE PLEASE FOCUS YOU SHOULD ENTER AN ARRAY AND THEN THE PROGRAM GIVE OUTPUT( TRUE/ FALSE) QUIZ 8 Array Challenge Have the function ArrayChallenge(arr) take the array of numbers stored in arr and return the string true if any two numbers can be multiplied so that the answer is greater than double the sum of all the elements in the array. If not, return the string false. For example: if arr is [2, 5, 6, -6, 16, 2,...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...