title: MIPS questions about writing assembly to call functions on an array
body: <p>I currently am taking a course in assembly and am having trouble with the following assignment. </p>
  
  <p>Write a program that reads (with an appropriate prompt) a sequence of 20 integers and stores them in an array, and then calls the following three functions and prints the results in a readable format. </p>
  
  <p><strong><em>The three functions are:</em></strong> 
  <strong>smallestLargest:</strong> computes the smallest and the largest values in the array. 
  <strong>divisible:</strong> computes the number of integers in the array which are divisible by 4
  <strong>SumProduct:</strong> computes the sum and the product of the integers.</p>
  
  <p>I wrote assembly code (below) to try to solve this problem but I can't get the right output except for the largest number in the array. Everything else is giving me a problem. I have no clue what's wrong, and I've been working on this for the past week and a half, so any help would be greatly appreciated.</p>
  
  <pre><code>.data
  array:  .space 80
  newLine:.asciiz "\n"    # I will use it to start a new line
  space:  .asciiz " "     # I will use it to have a space
  Prompt: .asciiz "\n Enter an integer: "
  Result1:.asciiz "The smallest value in the array is "
  Result2:.asciiz "The largest value in the array is "
  Result3:.asciiz "The number of integers divisible by 4 is "
  Result4:.asciiz "The sum of the integers is "
  Result5:.asciiz "The product of the integers is "
          .globl  main
          .text
  main:   
          li $t0,20       # $t0 keeps track of the number of integers to be read
          la $t1,array    # loading the starting address of an array
  loopQ:  
          la $a0,Prompt
          li $v0,4
          syscall
          li $v0,5        # reading an integer
          syscall
          sw $v0,0($t1)   # storing the integer entered
          add $t0,$t0,-1  # decrement the number of integers by one
          add $t1,$t1,4   # load the address of the next integer
          bgtz $t0,loopQ  # branch to read and store the next integer
          li $t0,20
          la $t1,array
  smallestLargest:
          lw $v0,0($t1)   # $v0 = Mem($t1)
          move $v1,$v0    # $v1 = $v0
          addi $t0,$t0,-1 # decrement $t0
          blez $t0,ret1   # while ($t0 &gt; 0)
  loopR:  
          addi $t1,$t1,4  
          lw $t2,0($t1)   # load $t1 to $t2
          bge $t2,$v0,next# if ($t2 &lt; $v0)
          move $v0,$t2    # $v0 = $t2
          b chk
  next:
          ble $t2,$v1,chk # if ($t2 &gt; $v1)
          move $v1,$t2    # $v1 = $t2
  chk:
          addi $t0,$t0,-1 # decrement t0
          bgtz $t0,loopR
  ret1:
          li $v0,4        # system call code for print_str
          la $a0,Result1  # load address of Result1 into $a0
          syscall
          move $a0,$v0    # move value to be printed to $a0
          li $v0,1        # system call code for print_int
          syscall
          la $a0,newLine  # start a new line
          li $v0,4
          syscall
          li $v0,4        # system call code for print_str
          la $a0,Result2  # load address of Result2 into $a0
          syscall
          move $a0,$v1    # move value to be printed to $a0
          li $v0,1        # system call code for print_int
          syscall
          la $a0,newLine  # start a new line
          li $v0,4
          syscall
          li $t0,20       # initialize length parameter
          la $t1,array    # initialize address parameter
  Div4:
          li $v0,0        # $v0 = 0
          li $t3,3        # $t3 = 3
          b skip
  loopS:
          lw $t2,0($t1)   # $t2 = Mem($t1)    
          addi $t1,$t1,4  # $t1 = $t1 + 4
          and $t4,$t2,$t3 # $t4 = $t2 &amp; $t3
          bnez $t4,skip   # if ($t4 == 0) 
          addi $v0,$v0,1  # $v0 = $v0 + 1
  skip:
          addi $t0,$t0,-1 # $t0 = $t0 - 1
          bgez $t0,loopS  # if $t0 &gt; 0
  ret2:
          li $v0,4        # system call code for print_str
          la $a0,Result3  # load address of Result3 into $a0
          syscall 
          move $a0,$v0    # move value to be printed to $a0
          li $v0,1        # system call code for print_int
          syscall
  SumAMult:   
          la $a0,newLine  # start a new line
          li $v0,4
          syscall
          li $v0,4        # system call code for print_str
          la $a0,Result4  # load address of Result4 into $a0
          syscall 
          li $t0,20       # initialize length parameter
          la $t1,array    # initialize address parameter
  
          jal sum         # call sum
  
          move $t1,$v0    # move value to be printed to $t1
          li $v0,1        # system call code for print_int
          syscall
          la $a0,newLine  # start a new line
          li $v0,4
          syscall
          li $v0,4        # system call code for print_str
          la $a0,Result5  # load address of Result5 into $a0
          syscall 
          li $v0,1        # system call code for print_int
          move $t1,$v1    # move value to be printed from $v1
          syscall
          li  $v0,10
          syscall
  sum:    
          li $v0,0        # $v0 will hold the sum
          li $v1,0        # $v1 will hold the product
  loopT:  
          blez $t0, retzz # If t0 &lt;= branch
          addi $t0,$t0,-1 # decrement loop count
          lw $t5,0($t1)   # get a value from the array
          addi $t1,$t1,4  # increment array pointer
  word:   
          add $v0,$v0,$t5 # add to sum
          # mult $v1,$t5  # multiply to product
          b loopT         # branch to loopT       
  retzz:  
          jr $ra
  </code></pre>
  
  <p>Below is the output I am getting after entering integers 1-20</p>
  
  <pre><code>The smallest value in the array is 4
  The largest value in the array is 20
  The number of integers divisible by 4 is 4
  The sum of the integers is 268501210
  The product of the integers is 268501238
  </code></pre>