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
1a. Using the MIPS program below (with bugs intact), determine the instruction format for each instruction...
1a. Using the MIPS program below (with bugs intact), determine the instruction format for each instruction and write the decimal values of each instruction field. addi $v0, $zero, 0 loop: lw $v1, 0($a0) sw $v1, 0($a1) sll $a0, $a0, 2 add $a1, $a1, $a0 beq $v1, $zero, loop 1b. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds...
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 MARS to write and simulate a MIPS assembly language program to swap two of the...
Use MARS to write and simulate a 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. Download the template file “P4_template.asm” provided on Blackboard. Add your code to this file. Do not modify any of the code provided in the file. The main function should: • Pass the starting address of the array in $a0....
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 procedure to calculate Average of numbers(integers) using Arrays. Send base address of array in...
Write a procedure to calculate Average of numbers(integers) using Arrays. Send base address of array in register $a1 and Array length in register $a2 to the procedure and return Average in register $v0 to main program. in assembly
Using MIPS Assembly language, sort 3 integers from greatest to smallest. Do this without using an...
Using MIPS Assembly language, sort 3 integers from greatest to smallest. Do this without using an array and instead use conditional branches, using the algorithm below: 1. Get Numbers From User 2.IF(#1 < #2) Swap the two numbers 3.IF(#2 < #3) Swap the two numbers 4.IF(#1 < #3) Swap the two numbers 5. Display Numbers in from greatest to smallest 6. End Program I have not been able to figure out the transfer of control when using branches and am...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...