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

This assignment asks to create an array with 10 integer elements. Ask user to input 10...
This assignment asks to create an array with 10 integer elements. Ask user to input 10 integer values. going through all the array elements and find/print out all the prime numbers. Instructions: Only use for loops for this lab (no need to create methods or classes for this assignment). Put a comment on each line of your codes, explaining what each line of code does. basic java, please
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;...
Create the following functions for an array in C++. Test with size 10, 10,000 and 100,000....
Create the following functions for an array in C++. Test with size 10, 10,000 and 100,000. Time each sort. Merge sort Insertion Sort Selection Sort         Bubble Sort Quick Sort PLEASE DO IT IN C++
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 #include #include 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; i < NUM; i++)...
5) Create the following in a Java program Create a scanner Prompt the user to enter...
5) Create the following in a Java program Create a scanner Prompt the user to enter the name where the box of mail is shipping from and create the variable and relate to scanner Prompt the user to enter the name of destination where the box of mail will be shipped and create the variable and relate to scanner Prompt the user to enter the weight of the package and create variable to relate to scanner Calculate cost of shipping...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT