MIPS添加奇数和偶数整数

时间:2016-04-21 06:35:20

标签: mips

我将使用MIPS在大小为100的预定义数组中添加奇数和偶数。该数组是: 7 99 100 90 34 30 10 29 64 29 88 19 55 98 5 86 68 5 29 60 51 100 39 55 24 52 46 58 76 16 59 37 47 7 57 31 11 23 82 88 76 86 22 73 63 66 41 98 36 82 51 54 7 95 71 19 36 67 46 30 27 36 91 60 94 86 33 47 22 36 64 78 55 83 86 21 22 78 62 29 93 27 93 11 18 47 87 44 18 39 50 90 42 4 3 80 61 99 55 81

我已经尝试了几天的代码,并且在运行总和程序后我没有得到输出。这是我的代码:

         .data

    array:   .word   7 99 100 90 34 30 10 29 64 29 88 19 55 98 5 86 68 5 29 60 51 100 39 55 24 52 46 58 76 16 59 37 47 7 57 31 11 23 82 88 76 86 22 73 63 66 41 98 36 82 51 54 7 95 7 1 19 36 67 46 30 27 36 91 60 94 86 33 47 22 36 64 78 55 83 86 21 22 78 62 29 93 27 93 11 18 47 87 44 18 39 50 90 42 4 3 80 61 99 55 81
        .text
  main:
        la         $a1,array                     # $a1 = &array
        li          $a0, 0              # $a0 = 0
loop:
        lbu       $t0,0($a1)                    # $t0 = Mem($a1)
        beqz     $t0, done
        addi     $a1, $a1, 2                   #
        andi     $t3, $t0, 1                    # $t3 = LSB of $t0
        bnez     $t3, odd                       # branch if odd
        bltz      $t0, loop
        add      $a0, $a0, $t0    # $t2 = $t2 + $t0       
        b         loop
          li      $v0, 4
       la      $t1, even
       syscall                                # print out "Even Sum = "
        li      $v0, 1
     move    $t1, $a0
       syscall                      # print out actual sum

odd:  
        bgtz      $t0, loop
       add      $a0, $a0, $t0    # $a0 = $a0 +$t0
        b          loop
           li      $v0, 4
       la      $t1, odd1
       syscall                                # print out "odd Sum = "
         li      $v0, 1
      move    $t1, $a0
        syscall                      # print out actual sum
done:
        li          $v0,     1          # Print result syscall(1)
        syscall
exit:
li      $v0, 10              # terminate program run and
syscall                      # Exit
                   .data
even:         .asciiz "Sum of Even = "
odd1:         .asciiz "Sum of Odd = " 

我收到的唯一输出是“0”。任何提示或指示将不胜感激。

0 个答案:

没有答案