In: Computer Science
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
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 .