Question

Write a MIPS code that finds the highest value, lowest value and average of 10 numbers...

Write a MIPS code that finds the highest value, lowest value and average of 10 numbers that are given as the global values (in the address range:0×10000000 to 0×10000009) and stores them in the next addresses and write a MIPS code module to sort the numbers in ascending order. Mention your sorting algorithm clearly.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below::

.data
array:   .word 123,-124,44,123,4490,2,85,2,13,55,0,0,0
size:   .word 10  
space:   .asciiz " "              
colonsp:   .asciiz ": "   #
  

prompt:   .asciiz "\nSorted array is : "
prompt1:   .asciiz "Array before sorting : "
min:   .asciiz "\nMinimum in array is : "
max:   .asciiz "\nMaximum in array is : "
avg:   .asciiz "\nAverage of the elements is : "
msg2: .asciiz "Enter element "
msg3: .asciiz " : "

.text
.globl   main
main:

lw $s0,size
li $s1,0 #index for loop
la $t0,array #save address of array
read_values_loop_exit:

li   $v0, 4
la   $a0, prompt1
syscall
jal   printArray           # call print routine.


la   $t0, array       # load array to $t0.
lw   $t1, size       # load array size to $t1.
li   $t2, 1           # loop runner, starting from 1.


li $t3,0
li $t7,0 #store max
li $t6,9999 #store min
li $t5,0 #store sum

loop:
mul $t4,$t3,4
add $t4,$t4,$t0 #get base address of a[i]
lw $t4,($t4) #get value of a[i]
blt $t4,$t7,notMax
move $t7,$t4 #set max
notMax:
bgt $t4,$t6,notMin
move $t6,$t4 #set min
notMin:
add $t5,$t5,$t4 #total sum
addi $t3,$t3,1
blt $t3,10,loop

li   $v0, 4
la   $a0, max
syscall
li $v0,1
move $a0,$t7
syscall
sw $t7,40($t0) #store max at index 11 of array

li   $v0, 4
la   $a0, min
syscall
li $v0,1
move $a0,$t6
syscall

sw $t6,44($t0) #store min at index 12 of array

li   $v0, 4
la   $a0, avg
syscall
li $v0,1
div $t5,$t5,$s0
move $a0,$t5
syscall
sw $t5,48($t0) #store avg at index 13 of array
sort_xloop:
la   $t0, array       # load array to $t0.
bge   $t2, $t1, sort_xloop_end   # while (t2 < $t1).
move   $t3, $t2       # copy $t2 to $t3.
sort_iloop:
la   $t0, array       # load array to $t0.
mul   $t5, $t3, 4       # multiply $t3 with 4, and store in $t5
add   $t0, $t0, $t5       # add the array address with $t5, which is the index multiplied with 4.
ble   $t3, $zero, sort_iloop_end   # while (t3 > 0).
lw   $t7, 0($t0)       # load array[$t3] to $t7.
lw   $t6, -4($t0)       # load array[$t3 - 1] to $t6.
bge   $t7, $t6, sort_iloop_end   # while (array[$t3] < array[$t3 - 1]).
lw   $t4, 0($t0)
sw   $t6, 0($t0)
sw   $t4, -4($t0)
subi   $t3, $t3, 1
j   sort_iloop       # jump back to the beginning of the sort_iloop.
sort_iloop_end:
addi   $t2, $t2, 1       # increment loop runner by 1.
j   sort_xloop       # jump back to the beginning of the sort_xloop.
sort_xloop_end:
li   $v0, 4           # 4 = print_string syscall.
la   $a0, prompt
syscall               # issue a system call.


jal   printArray           # call print routine.
exit:
li   $v0, 10           # 10 = exit syscall.
syscall               # issue a system call.

printArray:

la   $t0, array
lw   $t1, size
li   $t2, 0
printLoop:
bge   $t2, $t1, exitPrint
li   $v0, 1
lw   $a0, 0($t0)
syscall
li   $v0, 4
la   $a0, space
syscall
addi   $t0, $t0, 4
addi   $t2, $t2, 1
j   printLoop
exitPrint:
jr   $ra

output:

Add a comment
Know the answer?
Add Answer to:
Write a MIPS code that finds the highest value, lowest value and average of 10 numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Directions: Problem 1: Write (using pen-and-paper rather than code) the list after each pass of quick...

    Directions: Problem 1: Write (using pen-and-paper rather than code) the list after each pass of quick and merge sort for the following list of numbers. Assume that you are sorting the numbers into ascending order. For quick sort, assume that the first number from the sublist is chosen as the pivot. 54 17 21 18 4 7 19 41 Problem 2: Write the list after each pass of the quick sort algorithm for the following list of numbers, using the...

  • 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...

  • Consider the following python code that will sort the list of numbers using the Bubble Sort...

    Consider the following python code that will sort the list of numbers using the Bubble Sort algorithm def bubbleSort(alist): print(alist) for j in range (len(alist) - 1, 8, -1): for i in range(): if alist[i] > alist[i + 1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp print(alist) alist = [52, 25, 94, 17, 25, 52] bubbleSort (alist) print(alist) Sort the following series of values into ascending order using the Bubble Sort algorithm. Write out the complete row of...

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion 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...

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion 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...

  • So the assignment is to write a program that have the user entered 3 numbers and...

    So the assignment is to write a program that have the user entered 3 numbers and than it will sort it by ascending order. Here are the code I wrote, but it won't compiled and I kept on getting an error message "Using uninitiazed memory" for all the variables. Can someone please help take a look and see whats going on? #include <iostream> using namespace std; int main() { //variables int num1, num2, num3; int lowest, middle, highest; //user inputs...

  • Can you help me write a Python 3.7 code for this question? Write a program using...

    Can you help me write a Python 3.7 code for this question? Write a program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a list. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The...

  • 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...

  • Write and test a MIPS assembly language program to compute and display the first prime numbers...

    Write and test a MIPS assembly language program to compute and display the first prime numbers up to n where n is given. Set n to be 19 but the program should work of any value of n. For the program to identify primes, the easiest way is to use the algorithm: for (i = 2; i < x; i++)       if ((x % i) == 0) break;   //break out, not prime where x is the number you are checking...

  • Question 3 (10 points) Convert the following MIPS assembly code into machine language. Write the instruction...

    Question 3 (10 points) Convert the following MIPS assembly code into machine language. Write the instruction in hexadecimal. The opcode for sw 43 (101011). sw St1, -4(St3) Question 4 (10 points) Consider the following MIPS assembly code: addi $s3, $0, 5 addi $s1, S0, 3 addi Ss1, $s1, 2 beq Ss3, Ssl, target addi Ss1, Ss1, 1 target: add Ss3, Ss1, Ssl a. After running the code, what is the value of Ss3? b. If the memory address of the...

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