Question

In: Computer Science

Directions of assignment: - Create an array of words of size 10. - Prompt the User...

Directions of assignment:

- Create an array of words of size 10.

- Prompt the User to enter the 10 integers. Populate the array with the integers as they are entered.

- You MUST use indexed addressing to traverse through the array.

- Determine the maximum and the minimum values contained within the array and print them out.

Can you see what I am doing wrong here, the last part (to find the min and max) I can't seem to get right. Can you point me in the right direction. we need to use MIPS ASSEMBLY language, here is my code.


.text
.globl main

main:
              
li $t0, 0                                 # initilize array index value to 0
li $t1, 10                               # size of array is 10 (0 - 9)
li $t3, 0                               # initialize counter to zero
   
#Populate the array

while:
   la $a0, intPrompt                        # prompt for integer
   li $v0,4                                  # a0 = address of string
   syscall                                # v0 = 4, indicates display a String

   li $v0,5                                # enter input -> v0
   syscall                                # 5 is sys call for read int

   move $t2, $v0                              # store word int to a[i]
   sw $t2, array($t0)

   add $t0,$t0,4                          # move pointer ahead to next array element
   add $t3,$t3,1                          # increment counter
  
   blt $t3,$t1,while                      # branch to while if counter < size of array
      
# End While to populate the array

wend:      
   la $a0,title                           # Display "Array: "
   li $v0,4                                # a0 = address of message
   syscall
      
# Loop to print array values
   
li $t0, 0                                # initilize array index value back to 0
li $t3, 0                                  # initilize size counter back to zero
     
startPrint:
   lw $t2,array($t0)                        # load word a[i] into temp (t2)
   move $a0, $t2                           # move a[i] to a0 for display
   li $v0,1                                   # display a[i]
   syscall
     
   la $a0,comma                          # Display ", "
   li $v0,4                                    # a0 = address of message
   syscall                                    # v0 = 4 which indicates display a string
     
   add $t0,$t0,4                           # move pointer ahead to next array element
   add $t3, $t3, 1                         # increment counter

   blt $t3,$t1,startPrint                 # branch to startPrint if counter < size of array

endPrint:                              # End loop to print array values     
la $a0,crlf                             # Display "cr/lf"
li $v0,4                              # a0 = address of message
syscall                                    # v0 = 4 which indicates display a string


# min and max loop

li $t0, 0                             # initilize array index value back to 0
li $t3, 0                               # initilize size counter back to zero
sw $t5,array($t0)                       # t5 = min = a[0] (init)
sw $t6,array($t0)                     # t6 = max = a[0] (init)
newLoop:
   lw $t2,array($t0)                    # loads into t2 the first input
   bge $t5,$t2, notMin               # if t5 >= t2 goto notMin
   move $t5, $t2                       # move t2 to t5 if false
   j notMax                              # jump to notMax

notMin: ble $t6,$t2,notMax        # if t6 <= t2 goto notMax
   move $t6,$t2                        # move t2 to t6

notMax: add $t0,$t0,4               # move pointer ahead to next array element
   add $t3, $t3, 1                      # increment counter
   bnez $t0,newLoop

        la $a0,p1                         # Display "The minimum number is "
        li $v0,4                            # a0 = address of message
        syscall                           # v0 = 4 which indicates display a string
  
        move $a0,$t2                  # Display the minimum number
        li $v0,1
        syscall

        la $a0,p2                       # Display "The maximum number is "
        li $v0,4                          # a0 = address of message
        syscall                   # v0 = 4 which indicates display a string

        move $a0,$t3            # Display the maximum number
        li $v0,1
        syscall

li $v0,10                                # End Of Program
syscall

.data
p1:         .asciiz   "The minimum number is "
p2:         .asciiz   "\nThe maximum number is "
intPrompt: .asciiz   "Enter an Integer: "   # hold the prompt message for each int in the array
title:      .asciiz   "\nArray: "
crlf:       .asciiz   "\n"
comma:      .asciiz   ", "

array:      .word 40 # 10 words

Solutions

Expert Solution

Please up vote ,comemnt if any query . Thanks for Question . Be safe .

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

Remark :

you declared array : .word 40 its not good practice .first reserve 40 bytes for array like this :

array : .space 40   #reserve 40 bytes for array word takes 4 bytes so 10*4 =40 bytes

Inside max and min loop you mismatched if else condition . so i change it to simple if else working .

loop Code of max and min commented fully .

Program :

.data
       array:      .space 40 # 10 words
   p1:         .asciiz   "The minimum number is "
   p2:         .asciiz   "\nThe maximum number is "
   intPrompt: .asciiz   "Enter an Integer: "   # hold the prompt message for each int in the array
   title:      .asciiz   "\nArray: "
   crlf:       .asciiz   "\n"
   comma:      .asciiz   ", "


.text
.globl main

main:
            
   li $t0, 0                                 # initilize array index value to 0
   li $t1, 10                               # size of array is 10 (0 - 9)
   li $t3, 0                               # initialize counter to zero

   #Populate the array

while:
   la $a0, intPrompt                        # prompt for integer
   li $v0,4                                  # a0 = address of string
   syscall                                # v0 = 4, indicates display a String

   li $v0,5                                # enter input -> v0
   syscall                                # 5 is sys call for read int

   move $t2, $v0                              # store word int to a[i]
   sw $t2, array($t0)

   add $t0,$t0,4                          # move pointer ahead to next array element
   add $t3,$t3,1                          # increment counter

   blt $t3,$t1,while                      # branch to while if counter < size of array
    
# End While to populate the array

wend:    
   la $a0,title                           # Display "Array: "
   li $v0,4                                # a0 = address of message
   syscall
    
# Loop to print array values

li $t0, 0                                # initilize array index value back to 0
li $t3, 0                                  # initilize size counter back to zero
   
startPrint:
   lw $t2,array($t0)                        # load word a[i] into temp (t2)
   move $a0, $t2                           # move a[i] to a0 for display
   li $v0,1                                   # display a[i]
   syscall
   
   la $a0,comma                          # Display ", "
   li $v0,4                                    # a0 = address of message
   syscall                                    # v0 = 4 which indicates display a string
   
   add $t0,$t0,4                           # move pointer ahead to next array element
   add $t3, $t3, 1                         # increment counter

   blt $t3,$t1,startPrint                 # branch to startPrint if counter < size of array

endPrint:                              # End loop to print array values   
la $a0,crlf                             # Display "cr/lf"
li $v0,4                              # a0 = address of message
syscall                                    # v0 = 4 which indicates display a string


# min and max loop

li $t0, 0                             # initilize array index value back to 0
li $t3, 1                               # initilize size counter back to 1 to start from a[1]
lw $t5,array($t0)                       # t5 = min = a[0] (init)
lw $t6,array($t0)                     # t6 = max = a[0] (init)
#we already get a[0] in $t5 and $t6 so start loop from a[1]
addi $t0,$t0,4 #move pointer to next array element

       #start of while loop
       whileLoop:
               lw $t2, array($t0)#load current element $t2=a[i]     
               bgt $t2,$t6,max #if current array element greater than $t6 than $t2 is max so jump to max label
               bge $t2,$t5,skip #else current array element greater than or equal to $t2 >min go to skip label
               move $t5,$t2 #else min($t5)=$t2
               j skip #jump to skip label
           max:   #inside max label assign $t2 to $t6=$t2 now $t6 have max value
                  move $t6,$t2  
             
           skip:#inside skip label increment loop count and pointer
               add $t0,$t0,4                          # move pointer ahead to next array element
               add $t3,$t3,1                          # increment counte
             
               blt $t3,$t1,whileLoop #if $t3<$t1 go to whileLabel
#--------------------------------------------------------------Print minimum and maximum ---------------------------------

                            
                la $a0,p1                         # Display "The minimum number is "
                    li $v0,4                            # a0 = address of message
                    syscall                           # v0 = 4 which indicates display a string

                   move $a0,$t5                  # Display the minimum number
                           li $v0,1
                           syscall

                          la $a0,p2                       # Display "The maximum number is "
                          li $v0,4                          # a0 = address of message
                          syscall                   # v0 = 4 which indicates display a string

                          move $a0,$t6            # Display the maximum number
                         li $v0,1
                         syscall
                       
       #terminate Program
           li $v0,10# End Of Program
           syscall

Output :

Please up vote ,comment if any query .


Related Solutions

In C++, Create an array of 10 characters. Use a loop to prompt for 10 letters...
In C++, Create an array of 10 characters. Use a loop to prompt for 10 letters (a-z|A-Z) and store each in the array. If an invalid character is entered, re-prompt until a valid char is entered. After 10 characters are stored in the array, use a loop to print out all characters in the array from the first to the last element. Then, use another loop starting at the last element to print all characters in the array in reverse...
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
Create in java a program that will prompt the user to enter a weight for a...
Create in java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates infusion rates based on weight of patient in an interactive GUI application, label it HEPCALC. The patients’ weight will be the only entry from the user. To calculate the infusion rate you will multiply 12 times the weight divided by 50 for a total number. The end result will need to round up or down the whole number....
Given an array of Student type and size 10, create a linked list of students by...
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list #include<iostream> #include<string> #include<fstream> using namespace std; const int NUM = 10; struct Student{ string fName; string lName; Student * next; }; int main() {        Student stuArr[NUM];        ifstream myfile;        myfile.open("Test.txt");        for(int i = 0;...
Prompt the user for their name, get and store the user input. Prompt the user for...
Prompt the user for their name, get and store the user input. Prompt the user for their age, get and store the user input. We will assume that the user will enter a positive integer and will do no error checking for valid input. Determine and store a movie ticket price based on the user's age. If their age is 12 or under, the ticket price is $5. If their age is between 13 and 64, inclusive, the ticket price...
Create a function named getCreds with no parameters that will prompt the user for their username...
Create a function named getCreds with no parameters that will prompt the user for their username and password. This function should return a dictionary called userInfo that looks like the dictionaries below: # Administrator accounts list adminList = [ { "username": "DaBigBoss", "password": "DaBest" }, { "username": "root", "password": "toor" } ] Create a function named checkLogin with two parameters: the userInfo and the adminList. The function should check the credentials to see if they are contained within the admin...
World History 1112 Assignment #5 Directions: Create a timeline with the 10 key events and dates...
World History 1112 Assignment #5 Directions: Create a timeline with the 10 key events and dates of either the                          Enlightenment or Scientific Revolution Example: 1686 = Sir Isaac Newton publishes his “Principia Mathematic”. The Principia is   considered one of the most important works in the history of science.
Directions: Write a C++ program that will create an array of four integers. It will allow...
Directions: Write a C++ program that will create an array of four integers. It will allow the user to enter in four valid scores and store them in the array. ( valid range is 0 - 100) It will compute the letter grade based upon the four scores, namely, A = 90 - 100, B= 80- 89, C = 70-79, D = 60-69, otherwise F. It will display the scores and letter grade to the screen. NOTE: No menu is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT