首页 > 解决方案 > 使用 MIPS 计算单词数组中的元素数

问题描述

我正在尝试对 MIPS 进行编码以计算大于 x 且小于 y 的单词数组中的元素数,但代码似乎没有正确分支,我不断得到“count = 9”,即在这个案例,我列表中的元素数量。

.text       
        .globl __start 
__start:        # execution starts here  */

la $a0, ans # load str "count ="
li $v0, 4
syscall     
            la $t0,nums    # load array into address
            li $t1,0      # setting counter to 0
            addi $s0, $zero, 50    # adding constant of 50
            addi $s1, $zero, 100   #adding constant of 100
            lw $t5,count           # loading word "9"
            
again :         lw $t4 ($t0)      # load first element of array into t4
                bgt $t4,$s0,passif50       #comparing if first element is greater than 50
                

passif50 :      blt $t4,$s1,passif100      #comparing if first element is less than 100
                

passif100 :     add $t1, $t1,1             #incrementing counter by 1
                add $t0, $t0,4             #increment current array element pointer
                sub $t5,$t5,1              #deincrement "count" word by 1
                beqz $t5,done              #when "count" word is equal to 0, exit loop
        
                
done :          move $a0,$t1
                li $v0,1
                syscall                # print the count

                li $v0, 10
                syscall                # end program
        

     .data          
nums:   .word 55,68,2,60,2000,51,99,1,67
count:  .word 9
ans:    .asciiz "count = "
endl:   .asciiz "\n"

标签: mips

解决方案


推荐阅读