nCrを再帰的に計算する mips アセンブリ プログラムを作成しようとしています。
ドライバを含めてプログラム全体を書きましたが、正しく機能しません。すべての入力と出力が機能しますが、再帰アルゴリズムがクレイジーな数値を返します。たとえば、10 ではなく nCr ==268501120 です。
更新されたコード: http://pastebin.com/52ueQu99
これが私のアルゴリズムのほんの一部です:
nCk:
sub $sp, $sp, 16 #allocate the needed space in stack.
sw $ra, 0($sp) #save return address in first position
sw $t3, 4($sp) #save n in the stack
sw $t4, 8($sp) #save k in the stack
sub $t3, $t3, 1 #Subtract one from n
sub $t4, $t4, 1 #Subtract one from k
jal checkBounds #Check for end of recursion.
sw $v0, 12($sp) #copy returned 1 or 0 into stack.
lw $t3, 4($sp) #Load original n back into t3.
lw $t4, 8($sp) #Load original k back into t4.
sub $t3, $t3, 1 #Subtract one from n again. (n-1 step of recursive algorithm)
jal checkBounds #Check for end of recursion with n 1 number lower.
lw $t2, 12($sp) #Load the value held in the previously returned v0.
add $v0, $v0, $t2 #Add old returned value to new returned value.
lw $ra, 0($sp) #Load the original return address.
addi $sp, $sp, 16 #Add 16 more bytes to the stack.
jr $ra
checkBounds: #Check if program should still recurse
beq $t3, $t4, return1 #If n==k
beq $t4, $0, return1 #if k==0
li $v0, 0 #If (j!=k || k!=0){ return 0};
jal nCk
jr $ra
return1: #Returns 1
li $v0, 1
jr $ra