1

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
Selena Gomez
  • 69
  • 1
  • 7
  • Run your program in a debugger, place a breakpoint at a suitable location (e.g. `cmp %g2, a1`), check if the registers contain the values you expect them to. If they do, keep stepping through the code until things start to go wrong. If they don't, the problem is earlier in your program so you'll have to debug your loop to verify that it's working correctly. – Michael Mar 18 '14 at 13:54
  • I just double checked that the value of %g2 is correct and it is. For example if the user inputs the limit to be 9 then it would print out 1,1,2,3,5,8 and %g2 is the last value in that sequence which is 8 and i commented out my if then statements and just had it print out %g2 in place to make sure that it correct and it is. So the problem is my if then statements which are the code after the 3 rows of exclamation points. But I'm not sure why its not doing the comparison correctly. – Selena Gomez Mar 18 '14 at 15:42
  • Because for the if then statement i want it to print "The last number 8 is divisible by 8. and do that for each one of the fibonnaci sequence ranging from 1, 2584. So it should look at the last value (%g2) and if it is equal to 8 then it prints that statement above, if not it should proceed down until there is a match and print that statement – Selena Gomez Mar 18 '14 at 15:59
  • Seems to work fine for me, except for the fact that one case falls through to the other. For input <1597, it prints nothing, for input between 1597 and 2584 it prints the 1597 message, and for input >=2584 it prints both messages. Also see my comment on your original question on how to do this properly. – Jester Mar 18 '14 at 16:00
  • If i were to do it this way is there a reason why it keeps printing out both? – Selena Gomez Mar 18 '14 at 17:27
  • Before the `next` label, you should jump over to the end otherwise execution will continue and thus print things multiple times. – Jester Mar 18 '14 at 17:41
  • what architecture is this? You must tag it in the question – phuclv Mar 20 '14 at 00:20

1 Answers1

0

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
Jester
  • 56,577
  • 4
  • 81
  • 125
  • Sweet it works, can you give a little explanation for the ctz_loop, I understand the ctz_end already, its just the stuff up until there is what i need some clarification how that works? I want to understand the process so I can learn from you. Thanks, your the best. So %o2 = 0001, andcc %g2, %o2, %g0 will perform the AND operator for the last number in the fib sequence lets say 8 so in hex that is 1000. ANDCC 0001, 1000 will give you 0000 which means the zero flag=1 and the negative flag=0.So how does the flags relate to BNE so it can determine if it should take the branch or not to ctz_end? – Selena Gomez Mar 18 '14 at 19:43
  • `bne` is really just checking the zero flag, if it's not set then it jumps. In fact some assemblers accept it as `bnz` as well, which may be more readable in this case. – Jester Mar 18 '14 at 22:13
  • I just updated my code, and now after asking for the limit, then printing out the sequence, and printing out the last numbers highest power of 2 that is divisible and now it will asks if you want to print a different sequence ( Y or N ) answer. I did some if then statements to compare %g3 (userAnswer) to lower and uppercase of yes and no, if yes it should loop the whole program again, if no it just prints goodbye. But the program runs but just prints a 1. The error is somewhere in the bottom portion, I'm guessing I'm not comparing characters correctly? – Selena Gomez Mar 19 '14 at 18:31
  • If the user enters a different character than Y,y,N,or n for the answer then it should loop back to re-ask the user again if they still want to print another sequence. – Selena Gomez Mar 19 '14 at 18:49
  • You should load the user's answer as a character not a word. Also you forget to exit the program for the "no" cases. – Jester Mar 19 '14 at 22:41
  • I found out the problem is loading the userInput of the character because when i tried to print out %g3 (userInput of a character), it didn't print out anything. I know for a character we don't use .word 0 but instead use .byte 0 and to load userInput into a register I believe we use ldsb or ldub but i tried both and neither works. Also i used %c to store the character in the string to print out. But I'm not sure why @Jester – Selena Gomez Mar 19 '14 at 22:43
  • Also `scanf` is allowed to change `%o1`, so you should reload it. – Jester Mar 19 '14 at 22:47
  • Do you mean re-loading scanf into a register in each of the if then statements? well i just updated my code for you to see and i commented out the if then statements to print out %g3 and it still isn't printing out %g3. I think i am not loading it correctly or something. Is "ldub" the right command? – Selena Gomez Mar 19 '14 at 23:02
  • `ldub` is the right command, but you need to reload `%o1` as I said! – Jester Mar 19 '14 at 23:06
  • I reloaded %o1 as you said but still nothing. I made the edit in my code above. – Selena Gomez Mar 20 '14 at 00:12
  • Reloading after you used it to read the value already? Nice trick :) – Jester Mar 20 '14 at 00:40
  • Wrong, of course. You need to reload it before you use it for fetching the input. That is between the `scanf` and the `ldub`. – Jester Mar 20 '14 at 00:43
  • I made the adjustments but still nothing, i tried doing it another way by making input2: .asciz "%s". and TESTSTRING: .asciz "You entered the character: %s\n" and input3: .asciz " " to have it print out %g3 but still no luck. This sucks – Selena Gomez Mar 20 '14 at 01:10
  • http://www.flickr.com/photos/101040410@N07/13278521924/ I tried following this format but no luck – Selena Gomez Mar 20 '14 at 01:13
  • Works fine here: `You entered the character: n`. – Jester Mar 20 '14 at 01:42
  • really? with the code i updated above? hmm thats weird it isn't working for me. – Selena Gomez Mar 20 '14 at 04:40
  • alright i ended up doing something different and went with what i know (the flickr image) and finally i got it to print the usersInputCharacter (%g3).The code has been updated, now after i uncomment everything that is in the code for it to either loop through the code again if the user enters Y,y or it will print Goodbye if the user enters N,n or if a different character was entered then it should re-ask the user again. I know my if then statements are correct the only thing that could be the problem is when it is "yes" it must loop through the code again and i did "ba BIGLOOP" and put BIGLOOP – Selena Gomez Mar 20 '14 at 05:37
  • and put BIGLOOP right up near up top where my code starts, is that correct? will the program know to branch back to the top? because most of the time i see branching is jumping/skipping over a block of code but never to branch to the top? What a journey Jester, i appreciate all the time you put in to help me. – Selena Gomez Mar 20 '14 at 05:40
  • I posted the error in the code above all the way at the bottom. That is the error i get after i uncomment everything in the code above and then comment out TESTSTRING: .asciz up top and comment out the Print command for test string towards the bottom – Selena Gomez Mar 20 '14 at 15:32
  • Alright I ended up commenting out both of the yes portion in the if then statements to see if it will run the no portion and it did. So the problem is definitely the branch to the top/beginning of the code for yes and the branch to re-ask the user if they still want to print another sequence if they entered something besides Yy,n, or N. – Selena Gomez Mar 20 '14 at 15:39