MIPS中的无限循环

时间:2014-05-12 13:46:29

标签: assembly mips

我试图用MIPS汇编语言编写一个类的程序,它将读取一个单词的一系列整数并打印出最高和最低值。到目前为止,我一直试图让它打印出最高价值。我已经修改了课程中提供的一些代码,并使用qt SPIM逐步完成。我相信我的条件陈述永远不会被评估。我认为我没有加载下一个单词,我的比较从来没有导致程序增加。我已经尝试了很多故障排除而且卡住了。下面是完整的代码。我会评论我觉得错误的位置。从QT我可以看出我的循环是无限的。

.text

main:   
        la      $a0, TABLE      # Load a value for argument
        la      $a1, n          # Load the second argument
        jal     high_low    # jump to high_low subroutine

        move    $a0, $v0      # Load the return of subroutine into argument
        li      $v0, 1          # print out the result from the subroutine
        syscall

        b       exit

### high_low subroutine ###
### returns the sum of the highest and lowest integer:
        subu    $sp, $sp, 8     # Setup a stack frame by allocating space
        sw      $ra, 4($sp)     # Store the return address
        sw      $fp, ($sp)      # Store the old frame pointer
        addu    $fp, $sp, 8     # Reset the frame pointer to the bottom of current           frame

        move    $t0, $a0        # Load first argument into $t0
        move    $t1, $a1        # Load second argument into $t1
        li      $t2, 0          # Initialize the value of $t2, this will be the counter

 high_low_loop:
        bgt     $t2, $t1, counting_sum_end # If the first number is larger, the loop ends
        # is this how i would get to the next integer in table? I feel this is where
        # i am incorrect and thus my conditional is never evaluated. 
        addu    $t3, $t0, 4     # move next digit to be read
        bgt     $t0, $t3, G1    # if t0 is great increment
G1:      
        addi    $t2, $t2, 1     # Increment $t0

        bgt     $t3, $t0, G2    # if t3 is bigger move it to t1 & increment
G2:         
        move    $t3, $t0        # move to t0, if t3 is larger  
        addi    $t2, $t2, 1     # increment counter

        addi    $t2, $t2, 1     # increment counter
        b high_low_loop     #  and repeat the loop

counting_sum_end:
        move    $v0, $t0        # Load highest into $v0 for return
        lw      $fp, ($sp)      # Unpack the stack frame
        lw      $ra, 4($sp)     # load the return address into $ra
        addu    $sp, $sp, 8     # restore stack pointer

        jr      $ra



exit:           li $v0, 10 # load exit instruction
            syscall    # and end program

.data
#TABLE:    .word   3  -1  6  5  7  -3  -15  18  2 
#n:        .word   9
TABLE:  .word   3
n:      .word   1
# end of add.asm

如果你看到G1和G2,我觉得我必须把东西放回到循环的开头。例如:b high_low_loop,但我收到了语法错误。任何方向都会很棒,因为我对mips很新。

0 个答案:

没有答案