Question

Everything in my code is functioning up until the 3 rows of exclamation marks. It prints out the fibonacci sequence up until the limit that the user inputs is where the sequence will stop. But after the 3 rows of exclamation marks i want it to print a string using if multiple if then statements. But when i run it, it doesn't print that string for some reason. So the error is somewhere after the exclamation points. Im just not sure what is the problem. If(J >= 2584) then you print this statement, if(J >= 1597) then you print this , and so on and i want to do this with a bunch more if then statements. Thankyou for your help

.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
Was it helpful?

Solution

Knowing that the real problem to solve is printing the largest power-of-two divisor, here is a possible algorithm: for(mask = 1; (mask & x) == 0; mask += mask); The assembly version could look like:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top