MIPS问题实现循环

时间:2016-03-12 18:21:14

标签: assembly mips

我试图在MIPS程序集中实现一个循环但我却遇到了麻烦。我已经在每一步和原始问题上用我的尝试和逻辑评论了我的代码。

int a[20]; 
int b = 17;
while b < 22 do:{ 
    a[int(b / 2)] = b * 13; 
    b = b + 1 
}


.data
    a:      .space 80
    b_num:  .word  17

.text
    .globl main
    main:   la    $s0, a            # load base address of a into $s0
            lw    $s5, b_num        # load b into $s5

    Loop:   li $t3, 2           # load integer 2 into $t3
            li $t4, 13          # load integer 13 into $t4
            div $s5, $t3        # divide b by 2
            mflo $s3            # move quotient into $s3
            add $t1, $s3, $s3   # $t1 = 2*i
            add $t1, $t1, $t1   # $t1 = 4*i
            add $t1, $t1, $s0   # $t1 = address of a[i] , a[i] = a[b/2], added 
                                # base address of a with counter i which is b/2

            slti $s6, $s5, 22   # check if b < 22 and put a 1 or 0 into $s6
            bne  $s6, $zero, Exit # check if $s6 from previous inequality is 0 (False). If so, Exit program.
            mult $s5,$t4        # b * 13
            mflo $t5            # move answer to $t5
            sw  $t5, 0($t1)     # a[i] = $t5
            addi $s5, $s5, 1    # b = b + 1
            j   Loop

    Exit:
            li $v0, 10            # Get ready for a system call.
            syscall               # syscall number 10: End of program.

1 个答案:

答案 0 :(得分:0)

应该是

 beqz $s6, Exit

而不是

 bne  $s6, $zero, Exit 

如果你这样离开,那么只执行一次循环迭代。