def count_partitions(n, m):
if n == 0:
return 1
elif n < 0:
return 0
elif m == 0:
return 0
else:
l = count_partitions(n, m-1) + count_partitions(n-m, m)
return l
尝试将此python代码转换为MIPS程序集。我的汇编代码完成了它的工作,但在得到正确的答案后,它继续并变得荒谬的数字。
我尝试使用mars.jar进行调试,发现我的堆栈指针在某一点上没有得到更新。非常感谢。
我的代码:
.data
prompt1: .asciiz "\Enter an integer to partition (n): "
prompt2: .asciiz "\Enter an integer for size of the partition (m): "
default: .asciiz "\nstuff "
.text
main:
la $a0, prompt1
li $v0,4
syscall
li $v0, 5
syscall
move $t0, $v0 # $t0 = n
la $a0, prompt2
li $v0,4
syscall
li $v0, 5
syscall
move $t1, $v0 # $t1 = m
add $a0, $t0, $zero
add $a1, $t1, $zero
addi $sp,$sp,-4
sw $ra,0($sp)
jal count_partitions
lw $ra,0($sp)
addi $sp,$sp,4
move $a0,$v0
li $v0, 1
syscall #prints the default
li $v0, 10
syscall
count_partitions:
addi $sp,$sp,-16
sw $s1,12($sp)
sw $a1,8($sp)
sw $a0,4($sp)
sw $ra,0($sp)
bne $a0,$zero,case1
addi $v0,$v0,1
j return
case1:
slti $t0,$a0,0
beq $t0,$zero,case2
j return
case2:
bne $a1,$zero,case3
j return
case3:
addi $a1,$a1,-1
jal count_partitions
addi $t7,$zero,1
mult $v0,$t7
mflo $s1
addi $a1,$a1,1
sub $a0,$a0,$a1
jal count_partitions
addi $t7,$zero,1
mult $v0,$t7
mflo $t2
add $v0,$s1,$t2
return:
lw $ra,0($sp)
lw $a0,4($sp)
lw $a1,8($sp)
lw $s1,12($sp)
addi $sp,$sp,16
jr $ra