Menu

Is there a limit to nested Ifs? - Yes, the stack limit

Help
2022-07-02
2022-09-09
  • Jack Hoffnung

    Jack Hoffnung - 2022-07-02

    Resolved. See https://sourceforge.net/p/gcbasic/discussion/579126/thread/e272106415/#44cc


    Have code that uses multiple nested If-Then-Else-End If statements, from nests of two up to 16. Each group of nests generates a variable which is displayed on a 2x16 LCD. As I added more, larger groups of nests, the code would increasingly fail in that the PIC (16F88) would freeze, not displaying all 16 variables. Each time I reset the chip, a different number of variables would be displayed, rarely getting to all 16 variables. The code is using less than 50% of program memory, and less than 33% of RAM. This made me wonder if GCB just cannot handle all those Ifs. Any ideas appreciated.

     

    Last edit: Anobium 2022-09-09
  • William Roth

    William Roth - 2022-07-03

    @Jack

    Please post the code that fails.

     
  • Jack Hoffnung

    Jack Hoffnung - 2022-07-03

    Thank you; code attached.
    Project description: An external random number generator circuit feeds random bytes to PORTB, code checks for uniqueness, then displays bytes as printable ASCII characters.

     
  • Anobium

    Anobium - 2022-07-03

    You are running out of stacks in Newrandom

    You are calling and calling Newrandom . If you call recursively you must control the stacks.

    Consider this.

    sub Newrandom         ;runs then stops RNG, reads new rand and checks correct range
    
      set ClockControl on   ;allows RNG circuit to constantly send changing bits to PORTB
      wait 1 ms
      set ClockControl off  ;bits to PORTB do not change
      wait 1 ms
      rand = PORTB                  ;obtain random number
    
      Do while rand <33 or rand >126 or rand = 92 then ;limits rand range to Standard keyboard Ascii characters
        set ClockControl on   ;allows RNG circuit to constantly send changing bits to PORTB
        wait 1 ms
        set ClockControl off  ;bits to PORTB do not change
        wait 1 ms
        rand = PORTB                  ;obtain random number
      Loop
    
    end sub
    
     

    Last edit: Anobium 2022-07-03
  • Jack Hoffnung

    Jack Hoffnung - 2022-07-04

    Thanks very much! I have no formal computer science education, and your response led me to learn about how stacks work, and how deep recursion results in stack overflow.

     
    • Anobium

      Anobium - 2022-07-04

      Pleasure. Your coding is great. And, we all learn each day from each other. :-)

       
  • Jack Hoffnung

    Jack Hoffnung - 2022-07-05

    Anobium,
    Substituting your code completely eliminated the problem with the PIC freezing. Now, the code completes to display 16 characters often, but stops most often at 14 characters, sometimes at 15, sometimes at 13. I thought that perhaps one of the ASCII codes might result in truncation of the
    LCD display, but the range limitation (code 33-126 minus 92) seems appropriate. I tried adding time delays in the Display sub, but that didn't help. The randomness of the display has me foxed.

     
    • Anobium

      Anobium - 2022-07-05

      How are you ensuring that the stacks are not exceeded?

      When you say stop. .. do you mean there is no output?

      Can you post your latest code, please.

       
  • Jack Hoffnung

    Jack Hoffnung - 2022-07-05

    I am ignorant on how to ensure the stacks are not exceeded.
    By stop, I mean that after the NewPWbutton is pushed, sometimes only 13, or 14, or 15 characters are displayed on the LCD. I get a new set of characters each time I press the button, and often will get all 16 characters the code asks for, but not each time.
    Code attached. Thanks again for your help.

     
    • Anobium

      Anobium - 2022-07-05

      OK, I do not think it is the stacks at the moment.

      So, as it is hard for me to simulate PortB input. My insight is that the Do While is never exiting. I have had add ccount .. my guess is that this will loop for ever. Does it ?

       
  • stan cartwright

    stan cartwright - 2022-07-05

    dim aray(17) ;will store random values
    dim ccount as Word
    aray = 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ;16 values with 0's as placeholders

    if you dim aray(16) as byte then all elements will be 0
    for n=1 to 16:aray(n)=random:next n..... 16 different numbers in aray()

     
  • Anobium

    Anobium - 2022-07-06

    @Jack,

    This a hard nut to crack. What I thought would be a two minute job... was 2 hours.

    There is something wrong with your approach. Consider your approach.

      three:
        If rand <> aray(1) then                    ;if rand different than aray(1)
          If rand <> aray(2) then                  ;and (2) then
             aray(3) = rand : Display              ;store in aray(3), display it
          Else                                     ;if rand same as aray(1) and (2)
             Newrandom                             ;get a new random and
             Goto three                            ;re-test for uniqueness
          End If
        End IF
           Newrandom
    
      four:
    

    if 'rand <> aray(1)' where there are the equal the program will skip to four, leaving a char missing - hence the missing chars sometimes.


    I think you need to rewrite. Use for-next loops to walk the array checking each element is not used.

    Delete all the if-else's - try something like this. A proposed solution for you:

    • load the array with 16 chars
    • displays initial result
    • then loops thru the array until unique - using the flag valid. If a duplicate is found, swap it out, set the flag valid = 0 and reexamine.
    • display final result = unique
        'load 16 chars... may not be unique but it is simply sets the initial values
        for xloop = 1 to 16
            Newrandom
            aray ( xloop ) = rand
        Next
        for xloop = 1 to 16
          locate 0, xloop-1
          print chr(aray(xloop))
        next
    
        valid = 0
        Do while valid = 0 
          valid = 1
    
          'walk the array - changing duplicates until all are unique
          for xloop = 1 to 15
              for yloop = 2 to 16
                    do while ( aray(xloop) = aray(yloop) ) and yloop <> xloop
                      Newrandom
                      aray ( yloop ) = rand
                      valid = 0
                    loop
              next
          next
        Loop
    
        for xloop = 1 to 16
            locate 1, xloop-1
            print chr(aray(xloop))
        next
    
        wait 5 s
        cls
        for xloop = 1 to 16
          print chr(aray(xloop))
        next
    
     
  • Anobium

    Anobium - 2022-07-06

    Oh my... using the password generator would provide 50 thousand trillion years until someone could crack the password...

     

    Last edit: Anobium 2022-07-06
  • Chris Roper

    Chris Roper - 2022-07-06

    50 thousand trillion years until someone could crack the password...
    Unless they had a Quantum Computer

     
    • Anobium

      Anobium - 2022-07-06

      This password application is a good idea.

      Create password, store credentials in Progmem for later recall. A standalone password tool, very clever.

       
  • Jack Hoffnung

    Jack Hoffnung - 2022-07-07

    I REALLY appreciate all the ideas and effort in helping with this project! Prior obligations require a few day hiatus in my work on this, but I look forward to trying out the change in approach, and I will post the entire and circuit when I have it working properly. Thanks again.

     
  • stan cartwright

    stan cartwright - 2022-07-08

    I thought checking 16 numbers where different was easy... until you try it :)

     
  • Jack Hoffnung

    Jack Hoffnung - 2022-07-21

    Finally got this working well. Attached are the code, the complete circuit, and a bit of explanation on how the RNG portion of the circuit works.

     
    • Anobium

      Anobium - 2022-07-21

      A great complete project.

      Well done.


      May I move to the Demonstrations? You can edit and up date there.

       
  • Jack Hoffnung

    Jack Hoffnung - 2022-07-21

    Yes, and thank you for all your help with this project.

     

Log in to post a comment.