MIPS程序求数组的正和和和负和

MIPS程序求数组的正和和和负和,mips,Mips,下面是求正数和负数之和的代码。但是,代码在运行时有算术溢出(运行时异常为0x00400034)。基本思想是使用for循环,但出现了错误。我是否错过了执行for循环的任何元素 .data array: .word -13, 82, 56, 63, -54 length: .word 10 newLine: .ascii "\n" posSumMsg: .ascii "Positive value = " negSumMsg: .ascii "Negat

下面是求正数和负数之和的代码。但是,代码在运行时有算术溢出(运行时异常为0x00400034)。基本思想是使用for循环,但出现了错误。我是否错过了执行for循环的任何元素

  .data

    array: .word -13, 82, 56, 63, -54
    length: .word 10
    newLine: .ascii "\n"
    posSumMsg: .ascii "Positive value = "
    negSumMsg: .ascii "Negative value = "
    posSum: .word 0
    negSum: .word 0

.text
.globl main
main:
    la $t0, array #the starting address of the array
    li $t1, 0 # set the loop index, where i = 0
    lw $t2, length # length
    li $t3, 0 # initialize the sum to be 0 for pos_sum
    li $t5, 0 # initialize for neg_sum

    #start of loop
sumLoop:
    lw $t4, ($t0) #get the array[i]
    blt $t4, 0, getNegSum #jump to the negative sum calculation block for negative number
    bgt $t4,0, getPosSum #jump to positive sum calculation block for positive number

    #calcuates the sum for positive numbers
getPosSum:
    add $t3, $t3, $t4 #sum = sum + array[i]
    j loopback

getNegSum:
    add $t5, $t5, $t4

    #loopback of the array
loopback:
    add $t1, $t1, 1 #i = i+1
    add $t0, $t0, 4 # update the address of the array
    bnez $t4, sumLoop # stop the for loop if the last elemnt is a 0, otherwise loop back

    #store the sum
    sw $t3, posSum 
    sw $t5, negSum

    #print a message
    li $v0, 4
    la $a0, posSumMsg
    syscall

    #print value
    li $v0, 1
    move $a0, $t3 
    syscall

    #print a message
    li $v0, 4
    la $a0, negSumMsg
    syscall

    #print a value
    li $v0, 1
    move $a0, $t5
    syscall

    #Terminate the program
    li $v0, 10
    syscall
.end main

请看我的答案:与此不同,此代码使用零字终止符来决定何时退出处理数组元素的循环。但是,数据没有此终止符,因此将继续对数组末尾之前的字符串进行求和,将后面的字符串文字相加。