嗨代码要求用户输入限制,并且必须打印斐波那契系列序列直到该限制。例如,限制为10,应打印1,1,2,3,5,8。我相信我的数学计算正确,
while(f<=userInput) {
f=i+j
i=j
j=f
print(j) }
并且i = 0,j = 1的值。我相信问题在于我的格式2打印出序列,我不确定。当我运行该程序时,它只是打印&#34; 1&#34;就是这样。谢谢你的帮助!
.section ".data"
prompt: .asciz "\nEnter a limit on the largest number to be displayed: "
format: .asciz "%d%c"
format2: .asciz "Fibonnaci series: %d\n"
myString: .asciz "1 "
myString2: .asciz "\n"
.align 4
input: .word 0
nl: .asciz "\n"
define(f, l0)
define(i, l1)
define(j, l2)
.align 4
.section ".text"
.global main
main:
save %sp, -96, %sp
clr %f
clr %i
mov 1, %j !initialize j=1
set prompt, %o0 !point o0 to the prompt
call printf !call printf to print the prompt
nop
set format, %o0 !address of the format
set input, %o1 !address of the location for the max
set nl, %o2
call scanf !reads user input, coverts to a
nop !number and stores at the memory referenced by input
set format2, %o0
set input, %o1
ld [%o1], %o1 !userInput loaded into o1
mov %o1, %g1 ! g1 = userInput
set myString, %o0 ! print leading 1 in fib sequence
call printf
nop
test:
add %i, %j, %f ! f=i+j
cmp %f, %g1 !while( f<= userInput)
bg done ! reverse the logic of the test to branch over when f > userInput
nop
mov %j, %i ! i=j
mov %f, %j ! j=f
mov %j, %o1
set format2, %o0
call printf ! print J
nop
ba test
nop
done:
set myString2, %o0 ! double space after fib sequence
call printf
nop
ret
restore
答案 0 :(得分:0)
在循环开始时,您正在针对%o1测试%f,其中%o1是用户输入。然后在循环期间使用%o1,因此它不再包含用户输入。您需要先将用户输入值保存在某处。
答案 1 :(得分:0)
正如 The Dark 所说,调用约定允许被调用函数(在本例中为printf
)来更改%o
寄存器。这也适用于%o0
,这意味着您应该在循环中重新加载format2
。除此之外,您在标签nop
之前的分支延迟槽中缺少done
。有了这些改变,它对我有用。