Question

MIPS CODE required to write an assembly program to find the maximum of an array of integers by...

required to write an assembly program to find the maximum of an
array of integers by doing the following:

1. Prompt user to input array size n (n <= 10)
2. Prompt user to input element values of array A one by one
3. Display the result on the console.

This program must at least include one function. The main program will read the values
of the array (as user inputs the element values) and stores them in the memory (data
segments section) and at the end print the result. The function finds the maximum value
of the array.
For example, if the array size is 4 and the element values are
A = { 2 6 -1 5 },
the result is: 6

The messages showing on the console should be:

Please input the array size: 12
!!!Invalid, try again
Please input the array size: 4
Please input the element values:
2
6
-1
5
The result is:
6
1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
We did this in our lab just yesterday. Took me three hours to do it :(
I request you not to just copy-paste it as this wouldnt help you at all.
Summary:

Read 10 integers and store in the array A, call proc minmax( ), display minimum and maximum
Registers used:
$a0 = address of the array
$a1 = n (n=10)
$v0 = minimum
$v1 = maximum

Code:
main:
li $v0, 4 # print prompt1
la $a0, prompt1
syscall

li $t0, 0 # counter i = 0 (i= $t0)
li $t1, 10 # read 10 integers ($t1 = 10)
la $t2, A # $t2 points to address of array A

# Read 10 integers
loop:
la $a0, prompt2 # print prompt2 "Enter integer number"
li $v0, 4
syscall

li $v0, 1 # print the number of the input
move $a0, $t0 # $a0 = counter i
syscall

li $v0, 4 # print string system service
li $v0, 5 # read an integer
syscall

move $t3, $v0 # $t3 = $v0 = i integer
addi $t4, $t0, 4 # $t4 = offset of A[i]
add $t4, $t4, $t2 # $t4 = address of A[i], $t2= address of array A
sw $t3, 0($t4) # store i integer in A[i]
addi $t0, $t0, 1 # i = i + 1
blt $t0, $t1, loop # if (i < 10) then go to loop ($t0= 0, $t1 =10)

# Call proc minmax
la $a0, A # $a0 = address of array A
addi $a1, $0, 10 # $a1 = n = 10

lw $a0, 4($sp) # allocate 4 bytes for the stack
sw $ra, 0($sp) # push return address($ra) into the stack
jal minmax # call minmax
lw $ra, 0($sp) # pop $ra out of the stack

addi $sp, $sp, - 4 # deallocate 4 bytes from the stack
move $t0, $v0 # $t0 = $v0 = minimum of array A
move $t1, $v1 # $t1 = $v1 = maximum of array A

la $a0, prompt3 # print prompt3 "Minimum ="
li $v0, 5
syscall

li $v0, 1 # print the minimum of array A
move $a0, $t0 # $a0 = $t0 = minimum
syscall

la $a0, prompt4 # print prompt4 "Maximum ="
li $v0, 5
syscall

li $v0, 1 # print the maximum of array A
move $a0, $t1 # $a0 = $t1 = maximum
syscall

jr $ra # return to caller

# Find the min and max of array A
minmax:
lw $t0, 0($a0) # int min = A[0]; $t0=0
lw $t1, 10($a0) # int max = A[0]; $t1= 10
lw $t2, 1 # int i = 1; $t2= address of array A

loop1:
bge $t2, $a1, exit # while (i < n) $a1=n, i = $t2
addi $t3, $t3, 4 # $t3 = offset of A[i]
add $t3, $t3, $a0 # $t3 = address of A[i]
lw $t4, 0($t3) # t4 = A[i];
bge $t4, $t0, max # if (min > t4)
move $t0, $t4 # min = t4;

max:
ble $t4, $t1, next # if (max < t4)
move $t1, $t4 # max = t4;

next:
addi $t2, $t2, 1 # i = i + 1;
j loop1 # jump to loop1

exit:
move $v0, $t0 # $v0 = min($t0)
move $v1, $t1 # $v1 = max($t1)
jr $ra # return to caller





Note that I am posting our code (it even finds minimum of the array!) as it is.
Message me for any queries. :)
answered by: Easton
Add a comment
Know the answer?
Add Answer to:
MIPS CODE required to write an assembly program to find the maximum of an array of integers by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Need to write a MIPS assembly program that finds the minimum and maximum and sum of...

    Need to write a MIPS assembly program that finds the minimum and maximum and sum of a stored array. It also finds the locations of the minimum and maximum. The interaction between the main program and the function is solely through the stack. The function stores $ra immediately after being called and restores $ra before returning to main. The main program reserves a static C like array (index starts from 0) of 10 elements and initializes it. The maximum should...

  • Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers

    Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers...

    Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers using a recursive function that implements Euclid's GCD algorithm as described below. Your program should greet the user "Euclid's GCD algorithm", prompt the user to input two integers, and then output the result "Euclid's Greatest Common Divisor Algorithm" GCD(M,N) = M                      (if N is 0) GCD(M,N) = GCD(N, M % N)   (if N > 0) you may assume that inputs are non-negative name your assembly...

  • Write a function to have a user enter some number of integers into an array. The...

    Write a function to have a user enter some number of integers into an array. The integer values must be between -100 and +100 inclusive (+100 and -100 should be accepted as valid inputs). The integer array and the size of the array are passed into the function through parameters. Do not worry about includes. This is only a function, so there is no main routine. The function should fill the array with valid inputs. For invalid input values, inform...

  • Please write a MIPS program to prompt the user to input three positive integers and then print out their greatest common prime factor.

    Please write a MIPS program to prompt the user to input three positive integers and then print out their greatest common prime factor.What to submit:1. Your MIPS program file.2. The log file of simulation in SPIM. The log file should contain the Registers,  Text Segments, Data Segments, and Console.

  • - Write a program that performs several operations on an array of positive ints. The program...

    - Write a program that performs several operations on an array of positive ints. The program should prompt the user for the size of the array (validate the size) and dynamically allocate it. Allow the user to enter the values, do not accept negative values in the array. - Your program should then define the functions described below, call them in order, and display the results of each operation: a) reverse: This function accepts a pointer to an int array...

  • Write an assembly function to find the minimum number in an array of N integers. The inputs are: ...

    Write an assembly function to find the minimum number in an array of N integers. The inputs are: the address of an array and the number N; the output is the minimum value in this array. Please 1) Draw the stack frame of this function; and 2) implement the function in assembly and comment each line.

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • Write a program that asks the user to input 10 integers of an array. The program...

    Write a program that asks the user to input 10 integers of an array. The program then inserts a new value at position (or index) given by the user, shifting each element right and dropping off the last element. For example, in the sample input/output below, the program will insert the value 30 at index 3. All the previous numbers of the array starting from position 3 (i.e., 7 1 20 9 23 8 22 will be shifted one position...

  • Assembly Language////Write a program that read in 10 integers from the user. Save the numbers into...

    Assembly Language////Write a program that read in 10 integers from the user. Save the numbers into an array; reverse the array and display the reversed array. .data arrayInt DWORD 10 DUP(?) Your program consists of 4 procedures: 1. main procedure: call procedures getInput, reverseArray, displayArray 2. getInput procedure: prompt user to enter 10 integer numbers, save the numbers into the memory for the arrayInt 3. reverseArray: reverse arrayInt 4. displayArray: display the reversed array

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT