Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to print a full array in MIPS, and sum certain integers of an array
    text
    copied!<p>I am having an issue with the following code I am using for an assignment.. Note, I do not want anybody just giving me code, I am really trying to understand MIPS. I am using the QTSpim simulator to run my MIPS code. </p> <p>The following code is supposed to allow the user to enter 10 integers from the keyboard, then take those integers and sum the ones that are less than the first inputted integer (ie. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 would sum all numbers except 10, to equal 45). Then, the program should output the array in the order it was given. Currently, my code is allowing the user to enter 10 integers, but then funny things happen. It sums the numbers in a way that I cannot follow (the most common sums somehow being 0, 4, and 50), and will then only print out 4 integers for the array list (which seems to be as follows: firstNumber, secondNumber, lastNumber, 10) I am really confused as to why this happens. Also, for some instances of integers, it will create an infinite loop outputting 0. </p> <p>I've been at this for hours, can somebody please give me some advice or pointers? All help is appreciated. Thanks!</p> <pre><code># DATA DECLARATION .data request: .asciiz "Enter an integer:\n" sumLine: .asciiz "The sum is: " aList: .asciiz "The array contains the following: \n" return: .asciiz "\n" array: .word 0 aLength: .word 10 input: .word 0 count: .word 0 count2: .word 0 count3: .word 0 sum: .word 0 next: .word 0 first: .word 0 one: .word 1 # PROGRAM CODE .text .globl main # PROGRAM EXECUTION #-------------------------------------------------------------------------- # Procedure main # Description: Initializes registers and prints the final sum. # parameters: $s0 = address of array, $t0 = length # return value: $v0 = sum # registers to be used: $s0, $t0, $t1, $t2, $t4, $t5, $v0 #-------------------------------------------------------------------------- main: la $s0, array # s0 = array lw $t0, aLength # t0 = aLength lw $t1, count # t1 = count lw $t2, input # t2 = input lw $t3, count2 # t3 = count2 lw $t4, count3 # t4 = count3 lw $t5, sum # t5 = sum lw $t6, first # t6 = first lw $t7, next # t7 = next lw $t9, one # t9 = one beq $t1, $zero, readArray # if count=0, goto readArray procedure la $a0, sumLine # load line to print li $v0, 4 # print sum output line syscall lw $a0, sum # load sum to print li $v0, 1 # print sum syscall la $a0, return # load line to print li $v0, 4 # print return line syscall la $a0, aList # load line to print li $v0, 4 # print the array list line syscall j printArray #-------------------------------------------------------------------------- # Procedure readArray # Description: Reads integers from the keyboard. # parameters: $s0 = address of array, $t0 = length # return value: -------- # registers to be used: $v0, $a0, $t0, $t1, $t2, $s0 #-------------------------------------------------------------------------- readArray: beq $t1, $t0, sumSmallerThanFirst # if t1=t0 (count = aLength) goto sum procedure la $a0, request # load line to print li $v0, 4 # print request line syscall li $v0, 5 # read integer from keyboard syscall sw $v0, input # store integer to input lw $t2, input # t2 = input sw $t2, 0($s0) # array[i] = t2 addi $s0, $s0, 4 # increment array (i++) addi $t1, $t1, 1 # increment count (count+1) sw $t1, count # store count to t1 beq $t1, $t9, store # if t1=t9 (count = one) goto store j readArray store: lw $t6, 0($s0) # t6 = array[i] sw $t6, first # t6 = first j readArray #-------------------------------------------------------------------------- # Procedure sumSmallerThanFirst # Description: Sums the inputted integers if they are &lt; the first integer. # parameters: $s0 = address of array, $t0 = length # return value: ---------- # registers to be used: $s0, $t0, $t3, $t5, $t6, $t7, $t8, $0 #-------------------------------------------------------------------------- sumSmallerThanFirst: la $s0, array beq $t3, $t0, main # if count=length, goto main lw $t7, 0($s0) # t7 = array[i] sw $t7, next # t7 = next slt $t8, $t7, $t6 # if t7&lt;t6, t8=1 addi $s0, $s0, 4 # array[i++] addi $t3, $t3, 1 # count+1 sw $t3, count2 # store count2 to t3 beq $t8, $zero, sumSmallerThanFirst # if t8=0, goto top sum add $t5, $t5, $t7 # t5=t5+t6 (sum = sum + array[i]) sw $t5, sum # store sum to t5 j sumSmallerThanFirst #-------------------------------------------------------------------------- # Procedure printArray # Description: Prints out the array of inputted integers. # parameters: $s0 = address of array, $t0 = length # return value: ------------- # registers to be used: $v0, $t0, $t4, $t6, $s0 #-------------------------------------------------------------------------- printArray: beq $t4, $t0, Exit # if count=length, goto Exit lw $t7, 0($s0) # t7 = array[i] sw $t7, next # t7 = next lw $a0, next # load array[i] to print li $v0, 1 # print array[i] syscall la $a0, return # load line to print li $v0, 4 # print return line syscall addi $s0, $s0, 4 # array[i++] addi $t4, $t4, 1 # count+1 sw $t4, count3 # store count3 to t4 j printArray Exit: jr $ra # return </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload