Question

In: Computer Science

Write a program in MIPS to find the largest element of an array, the array size...

  1. 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 value in the array "
.text

main:
li $v0, 4
la $a0, enter  #asking the user to input how many numbers they would like to enter
syscall

li $v0, 5
syscall #user input

add $s1, $v0, $0 #declaring v0 to s1

blt $s1, $0, error #if # isn't +
bgt $s1, 10, error #if # is greater than 10
beq $s1, $0, error #if # is 0

add $s0, $s1, $s2
addi $s0, $s1, -50

la $s6, myarray

sw $t0, 32($s6)

swap:
add $t0, $a1, $0
loop:
beq $t0, $t1, done
addi $s0, $s0, 4
add $s2, $s2, $t5

j loop

done: 
li $v0, 4
la $a0, large
syscall
#need to print biggest array here#
li $v0, 10
syscall

error:
li $v0, 4
la $a0, invalid
syscall

j main

Solutions

Expert Solution

Please upvote ,comment if any query . i will change . Be safe .

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

this program find largest element from pre defined array of size with some defined values .

Program Plan :

  1. first create array of size 10 with some values.
  2. now prompt user to enter size of array between 0-10
  3. now store user size in $s1
  4. check size between 1-10 else go to main label
  5. inside INputLoop prompt user to enter array element and store them one by one
  6. after loop ends print largest element
  7. now load address of array in register $s0.
  8. store first element of array in $t0 using lw instruction .
  9. run a loop from 1 to size of array 10
  10. multiply loop index t1=1 by 4 $t3=1*4, 2*4
  11. now add address of array(base address) + t3 = Array[$t1(loop index)]
  12. using lw store element in $t2
  13. if $t2<= $t3 go to skip label (means go to next element )
  14. else make $t0=t2 now max element stored in $t0
  15. after loop completion print string largest element is
  16. and print value of $t0 (which have largest element)
  17. terminate program

Program :

.data
   myarray :       .word 0,0,0,0,0,0,0,0,0,0
   size_prompt: .asciiz "Enter array size : "
   large:            .asciiz "The largest element is : "
   array_prompt: .asciiz "enter array element: "
.text

   main:   


addi $v0, $0, 4 # system call (4) to print string
la $a0, size_prompt # put string memory address in register $a0
syscall # print string
  
addi $v0, $0, 5 # system call (5) to get integer from user and store in register $v0
syscall # get user input for variable "size"
addi $s1,$v0,0 #user input size in $s1
  
ble $s1,0,main #if user input size less than or equal to 0 go to main label
bgt $s1,10,main #if user input greater than 10 go to main label
  
  
li $t0,0 #loop count 0
la $s0,myarray #load addresss of my array in $s0

  
  
  
Inputloop:
beq $t0, $s1, endInputLoop #if $t0(loop index i)==$s1(size of array )
mul $t2, $t0, 4 #t2=$t0*4 index*4
  
addi $v0, $0, 4 #print array_prompt string
la $a0, array_prompt
syscall
  
  
  
addi $v0, $0, 5 # get input from user and store in $v0
syscall   
  
add $t3, $s0, $t2 #$t3= $s0(base address of array) + $t2(index+word offset(4))
sw $v0, 0($t3) #myArray[$t0]=$v0
addi $t0, $t0, 1 # i = i + 1
  
j Inputloop # jump InputLoop
  
endInputLoop:

   li $t0,0
  
loop: #loop label
beq $t1,10,printLargest #t1==10 go to printLargest label
mul $t3,$t1,4 #$t3=loop index*4
add $t3,$t3,$s0 #t3= address of first element($s0)+ $t3
lw $t2,0($t3) #$t2=myarray[$t1]
  
ble $t2,$t0,skip #if current element <= $t0(largest element) go to skip
  
addi $t0,$t2,0 #else $t0=$t2 make $t2 largest element
  
  
skip:#skip label
addi $t1,$t1,1 #increment loop count
j loop #jump to loop
  
  
printLargest:#print Largest label
la $a0,large #print large string using syscall 4
li $v0,4
syscall
  
addi $a0,$t0,0 #$a0=$t0
li $v0,1 #syscall 1
syscall
  
#terminate program
li $v0,10 #syscall 10 to terminate program
syscall
  


      
Output : myarray: .word 2,5,49,484,44,4,473,994,384,93

Please up vote ,comment if any query .


Related Solutions

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.
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 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.
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Question 6 Which of the following for loops will find the largest element in the array...
Question 6 Which of the following for loops will find the largest element in the array numbers, assuming numbers has already been assigned a collection of numeric values? Question 6 options: largest = None for i in range(len(numbers)): if largest is None and numbers[i] > largest: largest = numbers[i] largest = None for i in range(numbers): if largest is None and numbers[i] > largest: largest = numbers[i] largest = None for i in range(len(numbers)): if largest is None or numbers[i]...
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
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)...
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85....
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85.                                                                                                                                                                    Q2.Write a Java program to find element 54 in array 45,41,65,53,76,90 using Binary Search. (Hint: Binary search is applied to sorted array elements) Q4.   Write a java program to create array list subject        - add English, Maths, Science to the list        - add Computers at index 2        - display first occurrence index of Maths        - traverse the list using...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT