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 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]...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t specify a size and don’t initialize with values. Starting with 2, pushing these into the back of the vector:   2, 4, 6, 8, 10 vector capacity (array size) changes and is dependent on the compiler. The size of the list is now 5. The vector capacity (array size) is 6. The back element is: 10 The front element is: 2 Now deleting the value at...
Write a program in Easy68K: a) Define an array of numbers in the memory. b) Read...
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 python program to sum the prime numbers existing in an array . For instance...
Write a python program to sum the prime numbers existing in an array . For instance , if A = [4, 7, 12, 3, 9] the output should be 10
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT