MIPS初学者:计数数组中的0数量

时间:2018-11-19 06:50:21

标签: arrays mips counting

有人可以为我提供MIPS代码吗?我不明白为什么它不起作用。我想计算数组中零的数量。我没有正确遍历数组吗?

       .data
array:      .word 20, 0, 3, 0, 0, -4, 0, -30, 120, 0, 0 # 
array of integers
array_size: .word 11 # number of elements in array

        .text
        .globl main
main:
  la $t0, array      # array[0]'s address
  lw $t1, array_size # array_size
  li $t2, 0          # amount of zeroes = 0
  li $t3, 0          # i = 0

Lbegin:
  bge $t3, $1, Ldone # if(i >= array_size) goto Ldone 

  # array[i]  
  sll $t4, $t3, 2    # i x 4 (aka offset)
  add $t4, $t4, $t0  # array[0 + i]'s address
  lw  $t5, 0($t4)    # retrieve value array[i]

  beqz $t5, Lequal_zero # if(array[i] == 0) goto Lequal_zero
  b Li_increment        # goto i++

Lequal_zero:
  addi $t2, $t2, 1      # amount of zeroes + 1

Li_increment:
  addi $t3, $t3, 1      # i++
  b Lbegin              # loop again

Ldone:
  li $v0, 1
  move $a0, $t2
  syscall

  jr $ra

0 个答案:

没有答案