Menu

Elegantly setting the initial values of variables?

mkstevo
2017-12-05
2017-12-30
  • mkstevo

    mkstevo - 2017-12-05

    Does anyone have any clever idea or routines that ensure all variables are set to zero as and when the program starts?

    I try to remember to keep all variable declarations together at the top of my code, then call a 'StartUp' routine into which I copy and paste a list of my variable declarations, then convert them from:

    Dim Variable1 As Byte
    Dim Variable2 As Byte
    Dim VariableL As Long
    Dim VariableW As Word
    ...
    

    To:

    Let Variable1 = 0
    Let Variable2 = 0
    Let VariableL = 0
    Let VariableW = 0
    ...
    

    And so on, and so forth.

    I just wondered if there were any more elegant ways of achieving a similar result that might eliminate silly errors where I've later added a further variable, have forgotten to add it to my 'initialise' list and then used it in maths - causing an overflow - as it has not had the value anticipated.

     
  • mkstevo

    mkstevo - 2017-12-05

    Of course the best way to eliminate silly errors might be to keep me away from the keyboard!

     
  • stan cartwright

    stan cartwright - 2017-12-08

    If you know vars location and how many you could poke them with 0 in a loop.

     
    • Anobium

      Anobium - 2017-12-08

      Do not poke the variables address. If I could remove poke and peek I would.

      As you are using #option explicit - you know the vars so set to 0 if this is concerning you. Another, methoid would be to take the ASM pull out all the variables, including the system and librtary variables = 0.

       
      • mkstevo

        mkstevo - 2017-12-08

        "As you are using #option explicit - you know the vars so set to 0 if this is concerning you. Another, methoid would be to take the ASM pull out all the variables, including the system and librtary variables = 0."

        Would you be able to expand on this a little please?

        in my .asm file, I have some variables:

        FILE_NUMBER EQU 39
        NOW_PLAYING EQU 40
        PLAYTEST    EQU 41
        

        How would I then use this information to set their values to 0?

        I'll be honest, I was hoping someone might say "Why not use the overloaded declaration for variables?"

        And that I should use:

        Dim Variable1 As Byte = 0
        Dim Variable2 As Byte = 0
        Dim VariableL As Long = 0
        Dim VariableW As Word = 0
        

        Which would be both elegant, and easy for me to see if I'd missed one out.

         
  • stan cartwright

    stan cartwright - 2017-12-08

    "If I could remove poke and peek I would."..... and goto? NOoooo!
    ps does anyone use peek/poke in gcb? picaxe it was normal because you soon ran out of variable space, joy.

     

    Last edit: stan cartwright 2017-12-08
    • mkstevo

      mkstevo - 2017-12-08

      I must admit I try to avoid GoTo where possible. I'm not a fan of Peek or Poke either.

      In PicAxe Basic I've often used Poke to build up a 'buffer' for my LCD displays, building the display components up, Pixel by Pixel, only updating individual Pixels as required, then putting 'Peek' into a loop that outputs the entire display sequentially if a full refresh is required, or just individual Pixels if only single Pixels have changed. I found this to be quicker than any other method of updating the LCD with the limited processor speed available to PicAxe. It also allows some simple routines to be made that can be called with values for the start Pixel and number of Pixels to update set so that the required Pixels can be updated in memory with similar routines for writing as many Pixels as required when updating the display. Probably wouldn't be simpler for other people, but it suited me at the time...

       
  • stan cartwright

    stan cartwright - 2017-12-08

    Peek n poke is very familiar to me from the 80's but this is 2017. Best never mentioned but the original post asked. Funny, I thought ram reverted to 0's when powered off.

     
  • stan cartwright

    stan cartwright - 2017-12-08

    Loads of fun mkstevo. I got a ssd1306 working and a graphic game working in picaxe but I nearly ran out off vars. That's why I appreciate the gcb glcd includes....and it's 100x faster.
    I haven't used peek/poke in gcb and don't intend to.

     
  • Geoffrey Younger

    Hello Stan,
    I just came across your recent comments on clearing variables at startup. Seeing that GCB accepts
    assembler; I use the following;-
    ;----------------Clear RAM 20h-7Fh in bank-0-------------------------
    mov FSR,#20h ;Start location
    STARTX ;
    clr INDF ;Clear RAM pointed to !
    inc FSR ;next location
    sb FSR.7 ;End of RAM ?
    jmp STARTX ;No,so return
    ;----------------All RAM now cleared !-------------------------------
    clr PORTC ;Port-C output pins=0
    ; setb LCD_PWR ;POWER SOURCE OF LCD MODULE
    ; call DELAY2 ;
    ;===============================================
    It's written in the old CVASM (8051 type) but the idea is there .
    regards, Geoff

     
  • jackjames

    jackjames - 2017-12-28

    FFor safety, only when needed, I reset the variable before using it.
    Without complicating my life ...

     
  • William Roth

    William Roth - 2017-12-28

    @mkstevo

    The method you are using is probably the best way if there are not a huge number of variables. However it is possible to clear ram memory using indirect addressing as aluded to in Geoffrey's post above. For example on an 18F27K40 this will clear the Ram in Bank 1

    SUB ClearBank1
         DIM FSR0 as WORD alias FSR0H,FSR0L
             LFSR FSR0, 0x100    
    CLEARNEXT:
       CLRF POSTINC0          ; Clear INDF  register then  inc pointer
       BTFSS FSR0H, 1         ; All done with  Bank1?
       BRA CLEARNEXT          ; NO, clear next
    End SUB
    

    This was take directly from the 18F27K40 datasheet page 118 (Example 10-5) with some slight changes to work with GCB.

    You should be able to use this method with a PIC16F chip as well, Just refer to the datasheet under "indirect addressing" ..

    I do not recommend this for the inexperienced or the faint of heart And by all means ..test, test. test before including in any final code.

     
    • joe rocci

      joe rocci - 2017-12-28

      Shouldn’t it also be possible to do it with GCB using Poke?

      From: William Roth
      Sent: Thursday, December 28, 2017 9:29 AM
      To: [gcbasic:discussion]
      Subject: [gcbasic:discussion] Elegantly setting the initial values of variables?

      @mkstevo

      The method you are using is probably the best way if there are not a huge number of variables. However it is possible to clear ram memory using indirect addressing as aluded to in Geoffrey's post above. For example on an 18F27K40 this will clear the Ram in Bank 1

      SUB ClearBank1
      DIM FSR0 as WORD alias FSR0H,FSR0L
      LFSR FSR0, 0x100
      CLEARNEXT:
      CLRF POSTINC0 ; Clear INDF register then inc pointer
      BTFSS FSR0H, 1 ; All done with Bank1?
      BRA CLEARNEXT ; NO, clear next
      End SUB
      This was take directly from the 18F27K40 datasheet page 118 (Example 10-5) with some slight changes to work with GCB.

      You should be able to use this method with a PIC16F chip as well, Just refer to the datasheet under "indirect addressing" ..

      I do not recommend this for the inexperienced or the faint of heart And by all means ..test, test. test before including in any final code.


      Elegantly setting the initial values of variables?


      Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/gcbasic/discussion/579125/

      To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/

       
  • William Roth

    William Roth - 2017-12-28

    Poke? Maybe.

    I was observing Evan's warning of ..... "Do not poke the variables address." . I'm sure he has a reason other than personal preference. I know it will affect code portability as will the code I posted

    However if you poke the addresses using a loop, how would you prevent from clearing the loop counter variable? I suppose you could dim it last or assign it a memory location above the other variabes ...?
    Like: "dim counter as byte at 100" and then clear all locations below 100 with poke?

    The method I posted above clears the first 512 bytes of ram on an 18F27K40 without using a loop counter variable and with no need to look at the ASM code to see where variables are asigned. It should be run at the very beginning of the program.

    I tested "poke" and it appears to work ok. Maybe Evan can ellaborate more on why not to use "poke"?

     
  • stan cartwright

    stan cartwright - 2017-12-28

    mega328p sram starts 0x100 and ends 0x08FF. Where does gcb store variables, from the start or end? Are vars stored in order they are declared?
    Do I need to know this as the number of vars depends on ram not like picaxe which has fixed number and then defined memory to peek/poke?

     
  • stan cartwright

    stan cartwright - 2017-12-28

    This doesn't print bytes 0x0102 to 0x0107 as 1,2,3,4,5 if they are the vars v1-v5 after dim ptr as word.

      #chip mega328p ,16
      #option explicit
    
       #define USART_BAUD_RATE 9600
       Dir PORTD.1 Out
       Dir PORTD.0 In
       #define USART_DELAY 10 ms
       #define USART_BLOCKING
       wait 500 ms
    
    dim ptr as word ;0x0100
    ptr=0x0102
    dim v1 as byte :v1=1 ;0x0102
    dim v2 as byte :v2=2
    dim v3 as byte :v3=3
    dim v4 as byte :v4=4
    dim v5 as byte :v5=5
    wait 3 s
    repeat 5
    HSerPrint peek (ptr)
    HSerPrintCRLF
    ptr++
    end repeat
    end
    
     
  • William Roth

    William Roth - 2017-12-28

    Stan,

    Have a look at the ASM file. It should tell you where the variables are stored. Click on Make ASM. or hit F7 ... When done (Shift F7) will open the resultant ASM file. Look for the lines with ".EQU."

    If you want to know where a particular variable is located in memory you can do this trick ...

     Hserprint @varname
    

    This will print the memory location of varname

     
  • stan cartwright

    stan cartwright - 2017-12-28

    Thanks but the point is I can get by without knowing where gcb vars are stored.. so far that is.
    I'll check asm equ
    edit seems they are assigned in alphabetic not as defined order.
    So poke with 0 to all ram or vars? as per the original post.
    I just define init var vals so academic.

    ;Set aside memory locations for variables
    .EQU    BOMB=256
    .EQU    BOMB_COUNTER=265
    .EQU    BOMB_OLD_Y=274
    .EQU    BOMB_X=292
    .EQU    BOMB_Y=310
    .EQU    CANNON_X=483
    .EQU    CHARCODE=484
    .EQU    CHARCOL=485
    .EQU    CHARCOLS=487
    .EQU    CHARCOL_H=486
    .EQU    CHARLOCX=488
    .EQU    CHARLOCX_H=489
    .EQU    CHARLOCY=490
    .EQU    CHARLOCY_H=491
    .EQU    CHARROW=492
    .EQU    CHARROWS=494
    .EQU    CHARROW_H=493
    .EQU    COL=495
    .EQU    CURRCHARCOL=496
    .EQU    CURRCHARROW=497
    .EQU    CURRCHARVAL=498
    .EQU    DELAY=499
    .EQU    DELAY_H=500
    .EQU    DX=328
    .EQU    DY=346
    .EQU    FRAME=501
    .EQU    FRAME_COUNT=502
    .EQU    GLCDBACKGROUND=503
    .EQU    GLCDBACKGROUND_H=504
    .EQU    GLCDCOLOUR=505
    .EQU    GLCDCOLOUR_H=506
    .EQU    GLCDDEVICEHEIGHT=507
    .EQU    GLCDDEVICEHEIGHT_H=508
    .EQU    GLCDDEVICEWIDTH=509
    .EQU    GLCDDEVICEWIDTH_H=510
    .EQU    GLCDFNTDEFAULT=511
    .EQU    GLCDFNTDEFAULTSIZE=512
    .EQU    GLCDFONTWIDTH=513
    .EQU    GLCDFOREGROUND=514
    .EQU    GLCDFOREGROUND_H=515
    .EQU    GLCDPRINTLEN=516
    .EQU    GLCDPRINTLOC=517
    .EQU    GLCDPRINTLOC_H=518
    .EQU    GLCDPRINT_STRING_COUNTER=519
    .EQU    GLCDX=520
    .EQU    GLCDX_H=521
    .EQU    GLCDY=522
    .EQU    GLCDY_H=523
    .EQU    GLCD_YORDINATE=524
    .EQU    GLCD_YORDINATE_H=525
    .EQU    HISCORE=526
    .EQU    HISCORE_H=527
    .EQU    HITS=528
    .EQU    ILI9341ADDRESSTYPE=529
    .EQU    ILI9341SENDBYTE=530
    .EQU    ILI9341SENDWORD=531
    .EQU    ILI9341SENDWORD_H=532
    .EQU    ILI9341TEMPOUT=533
    .EQU    INXRADIUS=534
    .EQU    INXRADIUS_H=535
    .EQU    LINECOLOUR=536
    .EQU    LINECOLOUR_H=537
    .EQU    OLDSPX=364
    .EQU    OLDSPY=382
    .EQU    OLD_CANNON_X=538
    .EQU    PIXEL=539
    .EQU    PIXEL_H=540
    .EQU    PRINTLOCX=541
    .EQU    PRINTLOCX_H=542
    .EQU    PRINTLOCY=543
    .EQU    PRINTLOCY_H=544
    .EQU    PTR=545
    .EQU    PTR_H=546
    .EQU    RADIUSERR=547
    .EQU    RADIUSERR_H=548
    .EQU    RANDOM=549
    .EQU    RANDOMSEED=550
    .EQU    RANDOMSEED_H=551
    .EQU    RANDOMTEMP=552
    .EQU    RANDOMTEMP_H=553
    .EQU    ROCKET_BUTTON_WAIT=554
    .EQU    ROCKET_OLD_Y=459
    .EQU    ROCKET_ON=467
    .EQU    ROCKET_X=471
    .EQU    ROCKET_Y=475
    .EQU    ROW=555
    .EQU    SPICLOCKMODE=556
    .EQU    SPICURRENTMODE=557
    .EQU    SPIRXDATA=558
    .EQU    SPITXDATA=559
    .EQU    SPRITEDATA_PTR=560
    .EQU    SPRITEDATA_PTR_H=561
    .EQU    SPRITE_HEIGHT=562
    .EQU    SPRITE_SIZE=563
    .EQU    SPRITE_SIZE_H=564
    .EQU    SPRITE_STATUS=400
    .EQU    SPRITE_WIDTH=565
    .EQU    SPRITE_X=566
    .EQU    SPRITE_X_H=567
    .EQU    SPRITE_Y=568
    .EQU    SPRITE_Y_H=569
    .EQU    SPX=409
    .EQU    SPY=427
    .EQU    STR=453
    .EQU    STRINGPOINTER=570
    .EQU    SYSARRAYTEMP1=571
    .EQU    SYSARRAYTEMP1_H=572
    .EQU    SYSARRAYTEMP2=573
    .EQU    SYSARRAYTEMP2_H=574
    .EQU    SYSARRAYTEMP3=575
    .EQU    SYSARRAYTEMP3_H=576
    .EQU    SYSARRAYTEMP4=577
    .EQU    SYSARRAYTEMP4_H=578
    .EQU    SYSARRAYTEMP5=579
    .EQU    SYSARRAYTEMP5_H=580
    .EQU    SYSARRAYTEMP6=581
    .EQU    SYSARRAYTEMP6_H=582
    .EQU    SYSARRAYTEMP7=583
    .EQU    SYSARRAYTEMP7_H=584
    .EQU    SYSARRAYTEMP8=585
    .EQU    SYSARRAYTEMP8_H=586
    .EQU    SYSCHARCOUNT=587
    .EQU    SYSLCDPRINTDATAHANDLER=588
    .EQU    SYSLCDPRINTDATAHANDLER_H=589
    .EQU    SYSREPEATTEMP1=590
    .EQU    SYSREPEATTEMP1_H=591
    .EQU    SYSREPEATTEMP2=592
    .EQU    SYSREPEATTEMP3=593
    .EQU    SYSREPEATTEMP3_H=594
    .EQU    SYSREPEATTEMP4=595
    .EQU    SYSSTRDATA=596
    .EQU    SYSSTRINGPARAM1=445
    .EQU    SYSVALTEMP=597
    .EQU    SYSVALTEMP_H=598
    .EQU    TEMP=599
    .EQU    TEMP2=600
    .EQU    X1=601
    .EQU    X1_H=602
    .EQU    X2=603
    .EQU    X2_H=604
    .EQU    XOFFSET=605
    .EQU    XOFFSET_H=606
    .EQU    XRADIUS=607
    .EQU    XRADIUS_H=608
    .EQU    Y1=609
    .EQU    Y1_H=610
    .EQU    Y2=611
    .EQU    Y2_H=612
    .EQU    YOFFSET=613
    .EQU    YOFFSET_H=614
    .EQU    YORDINATE=615
    
     

    Last edit: stan cartwright 2017-12-28
  • stan cartwright

    stan cartwright - 2017-12-28

    Using ssd1306 the 1K buffer is declared 1st with uno if include glcd 1st so nice to use that maybe.
    SSD1306_BUFFERALIAS=256

     
  • Gert vd Walt

    Gert vd Walt - 2017-12-30

    Great cow basic dont care in witch order or where you declare a variable. So you can do the following:

    Dim think as Byte
    think = 0
    Dim position as Byte
    position = 0
    Dim linesense as byte
    linesense = 0

    or simply:

    Dim think as Byte : think = 0
    Dim position as Byte : position = 0
    Dim linesense as byte : linesense = 0

    not as pretty as you wanted, but, I think, much better than messing with assembly.

     

Log in to post a comment.