Question

In: Computer Science

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

Solutions

Expert Solution

Given below is the code for the question. Please do rate the answer if it helped. Thank you.

.data
   A: .word 3, 5, 8, 10, 12, 2, 76, 43, 90, 44
   size: .word 10
.text
   la $a0, A
   lw $a1, size
   addi $a2, $zero, 12
   jal search # search for 12 in the array A of size 10
   move $a0, $v0 # subroutine search result is store in $v0; its value is moved to $a0
   li $v0, 1 # code to print integer value in $a0
   syscall
   li $v0, 10 # code to terminate program
   syscall

search:
   li $t0, 0 #index
   li $v0, 0 #set return value to false
loop:
   bge $t0, $a1, exit_search
   mul $t1, $t0, 4 #to get offset, multiply index by 4 since each integer in the array takes 4 bytes
   add $t1, $t1, $a0 #add base address to offset
   lw $t0, ($t1)
   beq $t0, $a2, found
   addi $t0, $t0, 1
   b loop
found:
   #store 1 in $v0
   li $v0, 1
  
exit_search:
   jr $ra
  
  
  
==================================
output
-----
1
-- program is finished running --


Related Solutions

Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
write a mips program to find the number of elements of the array that are divisible...
write a mips program to find the number of elements of the array that are divisible by 3.
Write a MIPS program that uses an implementation of quick sort to sort an array of...
Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language). Quicksort Implementation - C int partition(int arr [] , int left , int right) { int i=left, j=right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr [ i ] < pivot) i ++; while (arr [ j ] > pivot) j −−; if (i <= j)...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the 2 numbers. The difference between the 2 numbers (num1-num2) and (num2-num1) The value that is 305 more than the 1st number. The value that is 305 less than the 2nd number
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and pass the values to a function that does multiplication
Write a program in Easy68K: a) Define an array of numbers in the memory.
Write a program in Easy68K: a) Define an array of numbers in the memory. b) Read two numbers from keyboard. The first number is the size of the array and the second number is what index of the array you want to access. The index you entered can be larger than the array. c) Display the element indexed by (index % size) in the array. 
Write a MIPS program that multiples every element in an array of size 10 by 4...
Write a MIPS program that multiples every element in an array of size 10 by 4 without using the multiply instruction.
Rewrite your program for part 1. Do not declare the array globally, declare it in the...
Rewrite your program for part 1. Do not declare the array globally, declare it in the loop function. This now requires that you add two parameters to your fill array and print array functions. You must now pass the array name and array size as arguments, when the program calls these functions. The program has the same behavior as problem 1, but illustrates the difference between globally and locally declared variables. The program code for part 1 was: int Array[15]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT