我正在为卢卡斯序列实现一个mips程序。用户输入数字n,P和Q,并为V序列选择1,为U序列选择0。我在选择V序列时遇到问题只有在P为1时才返回P所有其他数字。我知道这是我遗漏的一点点,但我一直试图找到它几个小时。任何帮助将不胜感激。
addi $t1, $s0, 0 # load n into $t1
addi $t3, $zero, 0 # i = 0
jal lucasSequence # go to lucasSequence
end_loop:
la $a0, newline # print a newline \n
jal printString
j main # loop to main menu again
#############################################
# Procedure: lucasSequence #
#############################################
# - produces the Lucas sequence of the #
# first (U) or second (V) order for #
# given constants P and Q. #
# #
# The procedure produces all numbers #
# in the sequence U or V from n=0 #
# up to n=N. #
# #
# - inputs : $a0-integer N #
# $a1-constant P #
# $a2-constant Q #
# $a3-function U (0) or V (1) #
# - outputs: none #
# #
#############################################
lucasSequence:
loop:
move $a0, $t3 # n = i
beq $t3, $t1, end_loop # if i == n, quit.
jal lucasSequenceNumber # print the lucas sequence for N, P, and Q
addi $t3, $t3, 1 # i++
move $a0, $v0 # load int to print
li $v0, 1 # print int
syscall # print call
beq $t3, $t1, end_loop # don't print last comma
li $a0, ',' # load comma
li $v0, 11 # add to print
syscall # print comma
li $a0, ' ' # load space
li $v0, 11 # print space
syscall # print call
j loop # repeat until i = n
lucasSequenceNumber:
addi $sp, $sp, -8 # room for $ra and one temporary
sw $ra, 4($sp) # save $ra
move $v0, $a0 # pre-load return value as n
addi $t4, $zero, 1 # t4 =1
blt $a0, 2, rt # if(n < 2) return
sw $a0, 0($sp) # save a copy of n
addi $a0, $a0, -1 # n - 1
jal lucasSequenceNumber # lucas(n - 1)
lw $a0, 0($sp) # retrieve n
mul $v0, $v0, $a1 # P*lucas(n-1)
sw $v0, 0($sp) # save result of P*lucas(n - 1)
addi $a0, $a0, -2 # n - 2
jal lucasSequenceNumber # lucas(n - 2)
mul $v0, $v0, $a2 # Q*lucas(n-2)
lw $v1, 0($sp) # retrieve P*lucas(n - 1)
sub $v0, $v1, $v0 # P*lucas(n - 1) + Q*lucas(n - 2)
rt:
addi $t4, $zero, 1 # t4 = 1
beq $a3, $t4, return_V # user chose option 2
lucas_rt:
lw $ra, 4($sp) # restore $ra
addi $sp, $sp, 8 # restore $sp
jr $ra # back to caller
return_V:
bgt $a0, $t4, lucas_rt # if choice V and n > 1, got to return
beq $a0, $t4, one_v # if n = 1, go to one_v
li $a0, 2 # if n = 0, return 2
move $v0, $a0
j lucas_rt # back to caller
one_v:
addi $a0, $a1, 0 # return P
move $v0, $a0
j lucas_rt # go to return P call
答案 0 :(得分:2)
需要调整one_v函数,使其显示为:
one_v:
move $a0, $a1
move $a0, $v0
j lucas_rt