-1

Hello I'm trying to debug this code. It gives the wrong values for its registers. The code is to test some simple arithmetic and storing of values in different registers. Thank you for your help!

.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
  • _Where_ does it give you the wrong value? Be as specific as possible so that people doesn't have to trace your entire code. – Michael Apr 21 '13 at 07:42

1 Answers1

0

I'm not sure where the errors are, but in these cases the comments and code do not correspond:

1

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

should be

li $t0, 0       # $t0 = 0

2

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

should be

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

3

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

should be

li $t1, 0       # t0 = 0

4

Pushing on the stack is

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

Popping:

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

If you can provide more details on where exactly is goes wrong, we can help better.

Patrik
  • 2,695
  • 1
  • 21
  • 36