汇编代码如果那么声明

时间:2014-03-18 05:09:05

标签: assembly sparc

我的代码中的所有内容都在运行,直到3行感叹号。它打印出斐波那契序列,直到用户输入的限制为序列停止的位置。但在3行感叹号之后,我希望它使用if if then语句来打印一个字符串。但是当我运行它时,由于某种原因它不打印该字符串。所以错误就在感叹号之后的某个地方。我只是不确定是什么问题。如果(J> = 2584)然后你打印这个语句,如果(J> = 1597)然后你打印这个,依此类推,我想用更多的if if语句来做这个。谢谢你的帮助

.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

1 个答案:

答案 0 :(得分:0)

知道要解决的真正问题是打印最大的二次幂除数,这里有一个可能的算法:for(mask = 1; (mask & x) == 0; mask += mask); 程序集版本可能如下所示:

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