qtspim的MIPS代码

时间:2013-04-20 15:27:26

标签: mips spim

您好我正在尝试调试此代码。它为寄存器提供了错误的值。代码是测试一些简单的算术并将值存储在不同的寄存器中。谢谢你的帮助!

.ent main
main:
addi $a0, $0, 10        # r4 = 0 + 10
addi $a1, $0, 1         # r5 = 0 + 1
sltiu $s1, $a1, 2       # set if a1 < 2
add  $t0, $t0, $0       # $t0 = 0
add $t1, $t1, $a1       # $t1 = $a1 (t1 = 1)

$count:
add $t0, $t0, $t1       # $t0 = $t0 + $t1   (t0 += 1)
slti $s1, $t0, 10       # if(a1 < 10) s1 = 1 else s1 = 0

beq $s1, $a1, $count    # while to < 10
sub $s1, $s1, $a0       # s1 = s1 - a1
and $s1, $s1, $a0       # s1 = s1 $ a0
or  $s2, $0, $s1        # s2 = 0 | a0

j $store                # store result

$recount:
sub $s1, $s1, $a1       # t0 = 0

$store: 
sw $s1,0($29)           # store s1 on stack
slti $t3, $s1, 0        # if s1 < 0
beq $s1, $0, $recount   # goto recount
lui $t3, 5              # load 5 into $t3
slt $s3, $t3, $t0       # if t3 < t0
beq $s1, $0, $end       # goto end
add $t0, $t0, $0        # t0 = 0
sw $s1, 0($29)          # store $s3 in $s1
lw $s4, 4($29)          # pop stack

$end:
jr $31
.end main

1 个答案:

答案 0 :(得分:0)

我不确定错误的位置,但在这些情况下,注释和代码不对应:

1

add  $t0, $t0, $0       # $t0 = 0

应该是

li $t0, 0       # $t0 = 0

2

add $t1, $t1, $a1       # $t1 = $a1 (t1 = 1)

应该是

move $t1, $a1       # $t1 = $a1 (t1 = 1)

3

sub $s1, $s1, $a1       # t0 = 0

应该是

li $t1, 0       # t0 = 0

4

推进堆栈是

addi $sp, $sp, -4
sw $t0, 0($sp)

弹出:

lw $t0, 0($sp)
addi $sp, $sp, 4

如果您可以提供有关出错的详细信息,我们可以提供更好的帮助。