MIPS - How to compute sum of the array

363 views Asked by At

i have this MIPS program that lets the user to input 10 integers and this program should be able to compute and display its sum.

.data    
    array: .space 40        
    input: .asciiz      "Enter the 10 Elements: \n"    
    output: .asciiz     "\nThe sum is: "  
 
.text 
main:
    la $a0, input          # Loads address of 'input' to print
    li $v0, 4           # System call code for printing string
    syscall             # Prints string
    
    jal loop            # Jumps and links to 'loop' label
    li $v0, 10    
        syscall         # System call to exit program
     
loop:    
    beq $t0, 40, end         
        li $v0, 5           # System call code for reading integer
        syscall         # Reads the integer
       
        sw $v0, array($t0)      # Store word, store the values in RAM. $t0 is the index
        add $t0, $t0, 4     

        j loop          # Jumps to loop label
           
end:     
        la $a0, output      # Loads address of 'output' to print
        li $v0, 4       # System call code for printing string
        syscall         # Prints string
        
        add $a0, $zero, $t1  
        li  $v0, 1          # System call code for printing integer
        syscall             # Prints the sum
        
        jr $ra          # Returns the value 

although, my problem is that it doesn't display its sum but rather just 0 in the output:

Enter the 10 Elements: 
1
2
3
4
5
6
7
8
9
10

The sum is: 0

Whereas, it should be displaying the sum of all integers inputted by the user:

Enter the 10 Elements: 
1
2
3
4
5
6
7
8
9
10

The sum is: 55

Is the problem on the loop label? Or the way I store the numbers in the array?

I don't get it how the sum displays 0.

I'm still getting into MIPS Assembly Language so it would be very helpful to point out where I did it wrong. And any suggestions in improving the way I write my code in MIPS? Thank you.

0

There are 0 answers