Assembly 汇编代码If-then语句
我的代码中的所有内容都在运行,直到出现3行感叹号为止。它打印出斐波那契序列,直到用户输入的限制是序列将停止的地方。但是在3行感叹号之后,我希望它使用if-multiple-if-then语句打印一个字符串。但当我运行它时,它不会出于某种原因打印该字符串。所以错误在感叹号后面的某个地方。我只是不确定问题出在哪里。如果j>=2584,则打印此语句,如果j>=1597,则打印此语句,依此类推,我想用更多的if-then语句来执行此操作。谢谢你的帮助Assembly 汇编代码If-then语句,assembly,sparc,Assembly,Sparc,我的代码中的所有内容都在运行,直到出现3行感叹号为止。它打印出斐波那契序列,直到用户输入的限制是序列将停止的地方。但是在3行感叹号之后,我希望它使用if-multiple-if-then语句打印一个字符串。但当我运行它时,它不会出于某种原因打印该字符串。所以错误在感叹号后面的某个地方。我只是不确定问题出在哪里。如果j>=2584,则打印此语句,如果j>=1597,则打印此语句,依此类推,我想用更多的if-then语句来执行此操作。谢谢你的帮助 .section ".data" prompt:
.section ".data"
prompt: .asciz "\nEnter a limit on the largest number to be displayed: "
format: .asciz "%d"
format2: .asciz "%d "
myString: .asciz "\n1 " !print leading 1
myString2: .asciz "\n" !double space
string: .asciz "\nThe last number %d is divisible by %d\n""
prompt2: .asciz "\nDo you want to print a different sequence (Y/N): "
format3: .asciz "%s"
noString: .asciz "\nGoodbye.\n"
NOstring: .asciz "\nGoodbye.\n"
TESTSTRING: .asciz "You entered the character: %s\n"
input2: .asciz " "
.align 4
input: .word 0
nl: .asciz "\n"
! input2: .byte 0
! nl2: .asciz "\n"
define(f, l0)
define(i, l1)
define(j, l2)
define(g1, l3)
define(g2, l4)
define(g3, l5)
.align 4
.section ".text"
.global main
main:
save %sp, -96, %sp
!BIGLOOP:
clr %f
mov 1, %j
clr %i
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 = user input
set myString, %o0 !print leading 1
call printf
nop
test:
add %i, %j, %f !f=i+j
cmp %f, %g1 !while(f<=userInput)
bg done
nop
mov %j, %i !i=j
mov %f, %j !j=f
mov %j, %o1 !%o1=j
set format2, %o0
call printf
nop
mov %j, %g2
ba test
nop
done:
set myString2, %o0 !double space
call printf
nop
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!divisible
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
set 1, %o2
ctz_loop:
andcc %g2, %o2, %g0
bne ctz_end
nop
ba ctz_loop
add %o2, %o2, %o2
ctz_end:
set string, %o0
mov %g2, %o1
call printf
nop
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Do you want to print another sequence
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! invalidloop:
set prompt2, %o0 !do you want to print a different sequence
call printf
nop
set format3, %o0
set input2, %o1
call scanf
nop
set input2, %o1 <<<------------------------
ldub [%o1], %g3 !loading user answer to %g3
set TESTSTRING, %o0 !!!!!!!TestString
mov %g3, %o1 !!!!!!!TestStromg
set input2, %o1 !!!!!!!
call printf !!!!!!!
nop !!!!!!!
/*
cmp %g3, "y" !comparing answer to y
bne YES
nop
ba BIGLOOP !loop back to the beginning of the program
nop
YES:
cmp %g3, "Y" !comparing answer to Y
bne no
nop
ba BIGLOOP !loop baack to the beginning of the program
nop
no:
cmp %g3, "n" !comparing answer to n
bne NO
nop
set noString, %o0
call printf
nop
ba finish
nop
NO:
cmp %g3, "N" !comparing answer to N
bne invalid
nop
set NOstring, %o0
call printf
nop
ba finish
nop
invalid: !if neither Y,y,N,n was answered then it should ask if you still want to print another sequence again until the correct is chosen
ba invalidloop
nop
finish:
*/
ret
restore
!!!ERROR!!!!
!!!!!!!!!!!!
!!!ERROR!!!!
!Undefined first referenced
!symbol in file
!invalid /var/tmp//cc87QWAM.o
!BIGLOOP /var/tmp//cc87QWAM.o
!ld: fatal: Symbol referencing errors. No output written to mai4
!collect2: ld returned 1 exit status
知道真正要解决的问题是打印两个除数的最大幂,这里有一个可能的算法:formask=1;掩码&x==0;面具+=面具; 程序集版本可能如下所示:
string: .asciz "\nThe last number %d is divisible by %d\n"
...
set 1, %o2 ! mask
ctz_loop:
andcc %g2, %o2, %g0
bne ctz_end
nop
ba ctz_loop
add %o2, %o2, %o2 ! next bit
ctz_end:
set string, %o0
mov %g2, %o1
call printf
nop
在调试器中运行程序,在适当的位置(如cmp%g2,a1)放置断点,检查寄存器是否包含您期望的值。如果他们这样做了,请继续逐步检查代码,直到出现问题。如果没有,则问题出现在程序的前面,因此您必须调试循环,以验证其是否正常工作。我只是再次检查了%g2的值是否正确,是否正确。例如,如果用户输入的限制为9,那么它将打印出1,1,2,3,5,8和%g2是该序列中的最后一个值,即8,我注释了我的if-then语句,并将其打印出%g2,以确保其正确且正确。所以问题是我的if-then语句,它是3行感叹号后面的代码。但是我不确定为什么它不能正确地进行比较,因为对于if-then语句,我想让它打印最后一个数字8可以被8整除。对每一个从12584到12584的fibonnaci序列都这样做。因此,它应该查看最后一个值%g2,如果它等于8,则它将打印上面的语句,如果不匹配,则应向下打印,直到找到匹配项,然后打印该语句似乎对我来说很好,但一个案例与另一个案例不符。对于input=2584,它将打印两条消息。也可以看到我对你最初的问题的评论,关于如何正确地做到这一点。如果我这样做的话,有什么原因它会一直打印出来吗?很好,你能解释一下ctz_循环吗,我已经了解ctz_端了,它只是一些东西,直到我需要澄清它是如何工作的?我想了解这个过程,这样我可以向你学习。谢谢你,祝你一切顺利。因此%o2=0001,CC%g2,%o2,%g0将对fib序列中的最后一个数字执行AND运算符,比如说,以十六进制表示的8,即1000。和CC 0001,1000将给你0000,这意味着零标志=1,负标志=0。那么这些标志与BNE有什么关系,这样它就可以确定它是否应该将分支带到ctz_端?BNE实际上只是检查零标志,如果它没有设置,那么它会跳转。事实上,一些汇编器也接受它为bnz,在这种情况下,它可能更具可读性。我刚刚更新了我的代码,现在在询问限制后,打印出序列,然后打印出最后一个数字,即可除的2的最大幂,现在它会问你是否要打印不同的序列Y或N答案。我做了一些if-then语句来比较%g3 userAnswer的大小写是和否,如果是,它应该再次循环整个程序,如果不是,它只是打印再见。但程序运行时只打印1。错误在底部的某个地方,我猜我没有正确地比较字符?如果用户输入了不同于Y、Y、N或N的字符作为答案,那么它应该返回以再次询问用户是否仍要打印另一个序列。您应该将用户的答案作为字符而不是单词加载。此外,您还忘记退出“无”情况下的程序。