Philip Rego
Philip Rego

Reputation: 648

MIPS - Printing string causes error trying to print an array value

Without any strings this program runs fine. But when I added the block of code where is says "if I take this block out it runs" I get an error where it says "causes ERROR here ". The error I get is "Runtime exception at 0x00400028: address out of range 0x00000002"

I have no idea why I'm getting this error. I don't see how printing a string would give me an error trying to print an int from a stack.

I've been trying to solve this problem for hours and I haven't found much help or resources for MIPS so any help would be greatly appreciated and marked useful.

.data 
.align 2
list:  .space 40    #an array of 10 ints
blank: .asciiz " "  
mess: .asciiz "The values in the array are "    


.globl main
.text

main:
    jal read

    li $v0, 4
    la $a0, mess
    syscall

    jal print
    jal total
    jal average
    lw $a0, 0($sp)  #load avererage from stack
    li $t0, 1   #print it
    syscall     #causes ERROR here                     ############
    b done      #end program

#reads in 10 digits into array      
read:
    li $v0, 5       #prompts for int
    syscall
    sw $v0, list($t0)   #stores int in array

    addi $t0, $t0, 4    #holds the number 4
    blt $t0, 40, read   #repeats until array is full
    jr $ra          #return to caller

#prints array   
print:
    lw $a0, list($t1)   #loads int from array
    li $v0, 1       #print int
    syscall

    li $v0, 4       #if I take this block out it runs      ########
    la $a0, blank       #
    syscall         #

    addi, $t1, $t1, 4   #counter 
    blt $t1, 40, print  #repeats until array it full
    jr $ra          #return to caller

#adds the total of all array elements 
total:
    lw $t3, list($t2)   #gets ints from array 
    add $s0, $s0, $t3   #adds number to running total
    addi $t2, $t2, 4    #counter 
    blt $t2, 40, total  #repeat until all ints are added
    addi $sp, $sp, -4   #allocate space on array
    sw $s0, 0($sp)      #store total on stack
    jr $ra          #return to caller

#divides the total by 10, for the average       
average:
    lw $a0, 0($sp)      #gets the total from the stack
    addi $t4, $t4, 10   #10 to divide by
    div $a0, $t4 
    mflo $t5
    sw $t5, 0($sp)      #store aveg in stack
    jr $ra      

done:

Upvotes: 1

Views: 1474

Answers (1)

Patrik
Patrik

Reputation: 2691

You forgot to set $v0 to 1. I guess you set $t0 instead of $v0.

Upvotes: 1

Related Questions