Question

In: Computer Science

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.

Solutions

Expert Solution

Please up vote ,comment if any query . Thanks for question . Be safe .

Note : check attached image for output ,code compiled and tested in MARS MIPS simulator .

Program Plan :

  1. declare array of size 10 with some value in data section
  2. inside text section load address of array in $t0 register and length of array 10 in $t1
  3. run a loop till loop index from 0 -10
  4. load each element from address stored in $t0
  5. and divide by 3 store reminder from mfhi
  6. set instruction use when $t4(divide reminder) equal to 0 than set $t3=1 else 0
  7. now increment loop index and address of array by 4
  8. now use branch instruction loop index less than 10 if so jump to loop label
  9. after loop complete dispaly count of element divide by 3 and print a result string
  10. terminate program

Program :

.data #vaariable section
   #array of word and its length is 10
   array : .word 100,99,98,97,96,95,80,81,82,51
   #null terminated string
   result :.asciiz " elements of array divisible by 3."
  
.text #code section
   main:
       la $t0,array #$t0=address of array in $t0
       li $t1,10 #t1= length of array 10
       li $t2,0 #loop count index ,$t2(i)=0
       li $a0,0 #number of element divisible by 3 count store in $a0
   loop: #loop label
      
       lw $t4,0($t0) #$t4= value at address stored in $t0 = array[$t2]
       div $t3,$t4,3 # quotient($t3)=array[$t2]($t4)/3
       mfhi $t3 #store reminder in $t3 from mflo
      
       seq $t3,$t3,0 #if $t3 equal to 0 than set 1 into $t3
       add $a0,$a0,$t3 #if t3 is 0 than 0 will add in $a0 else 1 will be add in $a0
       addi $t2,$t2,1 #increment loop count
       addi $t0,$t0,4 #point to next array element 4 is word offset
       bne $t2,$t1,loop #if $t2(loop index) not equal to 10($t1)length of array jump to loop
      
       #when loop completes program control comes here
       li $v0,1 #syscall 1 to print integer $a0 already have divisible count
       syscall
      
       la $a0,result #a0=address of result
       li $v0,4 #syscall 4 to print string
       syscall
      
       #syscall 10 to terminate program
       li $v0,10
       syscall

Output : array : .word 100,99,98,97,96,95,80,81,82,51

Please up vote ,comment if any query .


Related Solutions

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...
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements....
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements. Start by creating 2 arrays that can each hold 10 integer values. Then, get values from the user and populate the 1st array. Next, populate the 2nd array with the values from the 1st array in reverse order. Then, average the corresponding elements in the 1st and 2nd arrays to populate a 3rd array (average the 1st element of the 1st array with the...
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
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...
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Write a program that allows the user to search the array to find the number entered
Write a program that allows the user to search the array to find the number entered
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
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...
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)...
Program Name: Divisible. Write a program that calculates the number of integers in a range from...
Program Name: Divisible. Write a program that calculates the number of integers in a range from 1 to some user-specified upper bound that are divisible by 3, the sum of those integers, and the number and sum of those integers not divisible by 3. Your program must display meaningful output which includes: the selected upper bound number of integers divisible by 3 sum of integers divisible by 3 number of integers not divisible by 3 sum of integers not divisible...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT