我的MIPS代码有什么问题? (总和,最大,分钟)

时间:2017-11-30 09:57:26

标签: assembly mips

我想打印数字列表的最大值,最小值和总和。但是,Qtspim会抛出异常错误和错误的地址错误。我试着找几个小时的问题,但我找不到它们。请帮我。 digit = $ s0,length = $ s1,sum = $ s2,max = $ s3,min = $ s4,i = $ s5

.data
digit:   .word 10, 12, 23, 28, 7, 39, 10, 11, 23, 12, 3, 4, 5, 1 
length:   .word 14 # the length of the digit list

.text
main:
    la $s0, digit           #s0 = digit
    la $s1, length          #s1 = length
    add $s2, $zero, $zero   #sum = 0
    lw $t0, 0($s0)          #t0 = digit[0]
    add $s3, $zero, $t0     #max = digit[0]
    add $s4, $zero, $t0     #min = digit[0]
    add $s5, $zero, $zero   #i = 0
Loop:
    beq $s1, $s5, Exit      #branch to Exit if(i==length)
    sll $t0, $s5, 2         #t0 = i*4
    add $t0, $t0, $s0       #t0 = digit + i*4
    lw $t1, 0($t0)          #t1 = digit[i]
    addi $s5, $s5, 1        #i++
    add $s2, $s2, $t1       #sum += digit[i]
    slt $t2, $s3, $t1       #t2 = 1 if(max < digit[i])
    beq $t2, $zero, Min     #if not, go to min
    add $s3, $zero, $t1
Min:
    slt $t2, $t1, $s4       #t2 = 1 if(min > digit[i])
    beq $t2, $zero, Loop
    add $s4, $zero, $t1
    j Loop
Exit:
    ori $v0, $0, 1
    # $a0 = sum
    add $a0, $s2, $zero
    syscall

    ori $v0, $0, 1
    # $a0 = max
    add $a0, $s3, $zero
    syscall

    ori $v0, $0, 1
    # $a0 = min
    add $a0, $s4, $zero
    syscall
.end

这是C代码:

#include <stdio.h>

int main()
{
    int digit[14] = {10, 12, 23, 28, 7, 39, 10, 11, 23, 12, 3, 4, 5, 1}; //digit = $s0
    int length = 14; //length = $s1
    int sum=0, max, min, i=0; //sum = $s2, max = $s3, min = $s4, i = $s5
    max=min=digit[0];

    while(i != length) 
    {
        if(max < digit[i])
            max = digit[i];
        if(min > digit[i])
            min = digit[i];
        sum += digit[i];
        i++;
    }

    printf("%d %d %d", sum, max, min);
    return 0;
}

0 个答案:

没有答案