Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a MIPS program that reads (with an appropriate prompt) a sequence of 20 integers and stores them in an array, and then calls the

Write a MIPS program that reads (with an appropriate prompt) a sequence of 20 integers and stores them in an array, and then calls the following two functions and prints the results in a readable format. The two functions are:

smallestLargest: computes the smallest and the largest values in the array.

oddEven: computes the number of even integers and the number of odd integers.

I wote a program below, but it doesn't work. Please fix it.

.data

arr: .word 0:20 len: .word 20 smallest: .word 0 largest: .word 0 oddCount: .word 0 evenCount: .word 0 prompt: .asciiz "Enter a number: " smallMsg: .asciiz " Smallest number is " largeMsg: .asciiz " Largest number is " evenMsg: .asciiz " The count of even numbers is " oddMsg: .asciiz " The count of odd numbers is "

.text #array loop la $t0, arr #array address li $t1, 1 #counter lw $t2, len loop1: bgt $t1, $t2, end_loop1 #prompt and read int li $v0, 4 la $a0, prompt syscall

#read int and store in array location li $v0, 5 syscall sw $v0, ($t0)

add $t1, $t1, 1 #increment counter add $t0, $t0, 4 #next array location b loop1

end_loop1:

#set up parameters to call function smallestLargest la $a0, arr lw $a1, len jal smallestLargest

#set up parameters to call function oddEven la $a0, arr lw $a1, len jal oddEven

#display the smallest number li $v0, 4 la $a0, smallMsg syscall

li $v0, 1 lw $a0, smallest syscall

#display the largest number li $v0, 4 la $a0, largeMsg syscall

li $v0, 1 lw $a0, largest syscall

#display the no. of odd numbers li $v0, 4 la $a0, oddMsg syscall

li $v0, 1 lw $a0, oddCount syscall

#display the no. of even numbers li $v0, 4 la $a0, evenMsg syscall

li $v0, 1 lw $a0, evenCount syscall

#exit li $v0, 10 syscall

#=================================

smallestLargest:

li $t1, 1 #counter #assume the 1st number is smallest and largest lw $t2, ($a0) #smallest lw $t3, ($a0) #largest

loop2: bgt $t1, $a1, end_loop2 lw $t0, ($a0)

bge $t0, $t2, check4large #is it more than or equal to previous smaller move $t2, $t0 #its smaller, save in $t2

check4large: ble $t0, $t3, next2 #is it less than or equal to previous large move $t3, $t0 #its larger than previous large, save in $t3

next2:

add $t1, $t1, 1 #increment counter add $a0, $a0, 4 #next array location b loop2

end_loop2: #save the computed values in memory sw $t2, smallest sw $t3, largest jr $ra

#=================================

oddEven:

li $t1, 1 #counter #assume the 1st number is smallest and largest li $t2, 2 #constant 2 in $t2 li $t4, 0 #count for odd numbers li $t5, 0 #count for even numbers

loop3: bgt $t1, $a1, end_loop3 lw $t0, ($a0)

div $t0, $t2 mfhi $t3 #get remainder into $t3 beqz $t3, incrEven #if remainder is zero, goto incrEvn add $t4, $t4, 1 #increment odd count b next3

incrEven: add $t5, $t5, 1 #increment even count

next3:

add $t1, $t1, 1 #increment counter add $a0, $a0, 4 #next array location b loop3

end_loop3: #save the computed values in memory sw $t4, oddCount sw $t5, evenCount jr $ra

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

8th Edition

013460153X, 978-0134601533

More Books

Students also viewed these Databases questions