Menu

arrays only for bytes?

Help
2021-05-20
2021-06-01
  • Jim Gregory

    Jim Gregory - 2021-05-20

    In the section of the documentation on variables , it states the array type is "A list of whole numbers ranging from 0 to 255". In the array section, an array is described as "essentially a list of byte numbers". In the Dim keyword section, it states the syntax when used with arrays is Dim array(size) [At location]; i.e., no type specification is allowed.

    All of this suggests arrays can only contain bytes.

    And if I do this:

    Dim test_array(5) As Word
    test_array = 400,0,0,0,0
    

    I get a compiler error:
    Error: Cannot store 400 in the byte variable POSTINC0

    But if I assign values to the array this way:

    Dim test_array(5) As Word
    For i = 1 to 5
     If i = 1 Then
          test_array(i) = 400
       Else
            test_array(i) = 0
       End 
    Next
    

    It compiles OK and test_array(1) will be 400 when printed over serial output.

    A similar test works when the array type is Long and Integer, too.

    I am assuming therefore that the documentation was written when arrays could only hold bytes, but this is no longer true--an array can hold any type within the limits of the device's RAM as long as values aren't assigned using a single-line statement. Is that correct?

    `

     
  • William Roth

    William Roth - 2021-05-20

    There appears to be some undocumented support for Word, Integer and Long Variable arrays. I am using the latest release candidate ( RC47)

    As you have seen this does not work:

     Dim MyArray(5) as Integer
     MyArray =  300, 301, -302,  -1292,  10
    

    However this seems to work OK.

    Dim MyArray(5) as Integer
    MyArray(1) =  300
    MyArray(2) =  301
    MyArray(3) =  -302
    MyArray(4) =  -1292
    MyArray(5) =  10
    
    For ii = 1 to 5
           Hserprint MyArray(ii)
          Hserprint ", "
    Next
    

    I would be very careful using this and would consider it "unsupported" until we can get an explanation from Anobium or Hugh. I doubt that someone forgot to update the help. I think it is more likely that there are specific reasons the Help does not address this.

    There could be issues on small devices and if page boundaries are crossed. As a minimum precaution I might assign the array address at the start of a unused memory page in the case of an 18F chip and at the start of an 80 Byte block of GPR in the case of a 16F. And then make sure that the array is not so large that it could cross the boundary to the next page.

    William

     

    Last edit: William Roth 2021-05-20
  • stan cartwright

    stan cartwright - 2021-05-20

    Why can't we dim arrays as strings ie dim data(5) as string : data(1)="hello" : data(2)="world" ?
    Too complicated working length of string? Why not multi dimensioned arrays?
    Pics now have more memory and the support old chips seems to restrict gcb with it's universal approach to ucontrollers.
    I do know how to get around this but it ought to be standard commands ..imho.

     
  • Jim Gregory

    Jim Gregory - 2021-05-20

    The single-line assignment seems to be a problem when using types other than bytes for arrays. For instance, the following:

    Dim i As Byte
    Dim test_array(5) As Word
    test_array = 0,0,0,0,0
    test_array(1) = 1000
    test_array(2) = 10000
    Do
         Wait 1 sec
        For i = 1 to 5
            HSerPrint test_array(i)
            HSerPrint ", "
        Next
        HSerPrintCRLF
    Loop
    

    Gives this:

    1000, 10000, 28503, 27762, 8548,
    1000, 10000, 28503, 27762, 8548,
    1000, 10000, 28503, 27762, 8548,
    1000, 10000, 28503, 27762, 8548,
    

    Oddly, if I blank the chip and remove the second assignments:

    Dim i As Byte
    Dim test_array(5) As Word
    test_array = 0,0,0,0,0
    Do
        Wait 1 sec
        For i = 1 to 5
            HSerPrint test_array(i)
            HSerPrint ", "
        Next
        HSerPrintCRLF
    Loop
    

    I now get this:

    0, 0, 28503, 27762, 8548,
    0, 0, 28503, 27762, 8548,
    0, 0, 28503, 27762, 8548,
    0, 0, 28503, 27762, 8548,
    
     
  • stan cartwright

    stan cartwright - 2021-05-20

    The help is valid but seems out dated. It's been updated to use the 328p as a simple examples
    probably cos they are popular.
    A lot of it is pic only and when it refers to programmable pin chips gets complicated.

     
  • Anobium

    Anobium - 2021-05-20

    Is this using RC47 ? As major changes have happened recently.

    The RC47 is the baseline that we are using.

     
  • Jim Gregory

    Jim Gregory - 2021-05-21

    This is using RC45 (the newest release candidate available for Linux), compiled on a RPi3 using the latest version of FreeBasic.. I got the same result using RC38, too.

     
  • Anobium

    Anobium - 2021-05-21

    Is this just a documentation error ? If yes ( or no ) then the Help can be updated by anyone.

    See https://github.com/Anobium/Great-Cow-BASIC-Help/tree/master/source and edit the source for others in the future. Even if this is an issue then updating the Help with help others in the future.

     
  • Jim Gregory

    Jim Gregory - 2021-05-21

    Yes, it is mostly a documentation error. I will submit a pull request.

    Before I do, how many bytes are required to store each element of an array composed of words, integers, or longs? I ask because the documentation currently states:

    The limit on array size varies dependent on the chip type. The 12F/16F series of chips the array limit is 80 elements. For the Atmel AVR or an 18F there is no limit other than free RAM however Great Cow BASIC limits the array size to 10,000 elements. If a memory limit is reached, the compiler will give an error message.

    I assume the 80 element lmit for 12F/16F chips is true for byte arrays, and would be less for other types?

    Does the 10,000 element limit for 18F and AVR devices hold for arrays of other types, too?

     
  • Anobium

    Anobium - 2021-05-21

    Before I do, how many bytes are required to store each element of an array composed of words, integers, or longs? I ask because the documentation currently states:

    Bytes - 1
    Word - 2
    Integers - 2
    Long  - 4
    

    The limit on array size varies dependent on the chip type. The 12F/16F series of chips the array limit is 80 elements. For the Atmel AVR or an 18F there is no limit other than free RAM however Great Cow BASIC limits the array size to 10,000 elements. If a memory limit is reached, the compiler will give an error message.

    Something better could be, please revise.

    The limit on the array size varies dependent on the chip type, the amount of RAM and the number of other variable you use in your program.

    Use the following simple program to determine the maximum array size. Set MAXSCOPE to a value which is less the total RAM. If the ARRAY does not fit reduce MAXSCOPE until the error message is not issued.

    You MUST set the imaxvarialbe to suit the size of the constant MAXSCOPE.

    #CHIP   12f1571
    #OPTION Explicit
    
      #DEFINE MAXSCOPE  111
      DIM     imaxscope As Byte
      DIM     test_array( MAXSCOPE )
    
    
      For imaxscope = 0 to MAXSCOPE
          test_array( imaxscope ) = imaxscope
      Next
    

    For the Atmel AVR, LGT 328p or an 18F array sizes are limited to 10,000 elements.

    If a memory limit is reached, the compiler will issue an error message.


    Does the cover everything?

     
  • David Stephenson

    It's good to know that arrays will hold variables bigger than byte (I've been splitting my words into a high and low byte arrays - don't suppose it makes much difference except for the extra programming). I also realize that the RAM banks on 16F chips are 80 bytes, but I have been regularly using larger byte arrays - so this limitation must have been circumvented some time ago.

     
  • Jim Gregory

    Jim Gregory - 2021-05-21

    Thanks for the example code. That's very useful and helpful. I will include it in my changes.

    Its worth noting that if you change the line:

    Dim test_array( MAXSCOPE )
    

    to

    Dim test_array( MAXSCOPE ) As Word
    

    the maximum array size MAXSCOPE that will compile without errors is halved from 111 to 55, as expected. For an array of longs, it's 27.

     
  • William Roth

    William Roth - 2021-05-22

    So If I understand correctly, setting the array elements via CSV line entries only supports byte sized elements. In other words this will continue to fail.

    Method1 :

      Dim MyArray(20 )  as WORD
       MyArray = 1000,  2000,  3000, 4000
    

    FAILS with Compiler Error

    The ONLY way to put the elements in to Word, Integer, and Long arrays is like this.

    Method 2:

     Dim MyArray(20) as Word
     MyArray(1) = 1000
     MyArray(2) = 2000
    MyArray(3) = 3000
    MyArray(4) = 4000
    

    It would seem to me that if Method 2 works with Word, Integer and Long elements then Method 1 could also be made to work similarly.

    IMO this is more than just a Help File issue. This is a failure of a method that should work as expected.

     
  • Anobium

    Anobium - 2021-05-24

    @Jim Gregory

    All your changes are published. See https://github.com/Anobium/Great-Cow-BASIC-Help/wiki

    Huge Thank you!

     
    • Jim Gregory

      Jim Gregory - 2021-05-25

      No problem. Glad I could be of help!

       
  • mmotte

    mmotte - 2021-05-24

    You have added functionality to arrays but you still don't have a basic functionality that other languages have of array(0). It that because of backward compatibility? Why can't the zeroth element be used? and /or available?

    Mike

     
    • Anobium

      Anobium - 2021-05-24

      @mmotte It may be worth while doing empirical testing. Test across the range of chips (10f, 12f, 16f,18f and AVR) writing and testing to array element zero. Test using all types of array variables.

      As I see it.,, empirical testing may reveal that byte array less than 255 elements are very different from larger byte arrays (because element zero cannot hold the array size!) and non-byte arrays are different from byte arrays.

      The docs may be incorrect but it is worth empirical testing to get to the truth.

       
  • mmotte

    mmotte - 2021-05-24

    @Anobium I remember having this conversation a couple years back. It was explained to me that there is no zeroth element. But i have used it for programs. Not lately though.

    Yes , testing

    BR
    Mike

     
    • Anobium

      Anobium - 2021-05-24

      Testing and update the docs... as there is a zero element (this rules for usage) and things have changed over the years.

      Please test using RC47. No point in testing any early version.

       
  • William Roth

    William Roth - 2021-05-25

    The zero element CAN be used. Just not when using the CSV line method of adding elements

    Here is one way.

     #Define LastArrayElementLoc 8
    DIM Test_Array(8)                'Actually 9 bytes
    Test_Array = 1,2,3,4,5,6,7,8
    Test_array(0) = 0               ' Overwrite   location 0 with data
    
    Dim ii
    For ii = 0 To LastArrayElementLoc
          Hserprint Test_Array(ii)
          HSerprint ", "
    Next
    
     

    Last edit: William Roth 2021-05-25
  • William Roth

    William Roth - 2021-05-25

    Here's another example of using loc 0 for data. This code snippet will read the first block of Program Flash Memory into a Buffer (array) and then from the Buffer ... display it to a Terminal Application.

    #Chip 18F16Q41, 16
    #CONFIG MCLRE=EXTMCLR
    #Startup InitPPS, 85    
    #OPTION EXPLICIT
    
    #DEFINE USART_BAUD_RATE 19200
    #DEFINE USART_TX_BLOCKING
    Wait 500 ms
    
    Dim Buffer(256)  ' Array
    Dim ii as word
    
    TBLPTRU = 0
    TBLPTRH = 0
    TBLPTRL = 0
    
    For ii  = 0 TO 255
           TBLRD*+
           Buffer(ii) = Tablat
    Next
    
    For ii = 0 to 255
           Hserprint hex(Buffer(ii))
           Hsersend 32
          IF II MOD 16 = 15  then HSerPrintCRLF
    Next
    
    Do
    loop
    
    SUB INITPPS
            Dir PORTB.6 OUT
            SET PORTB.6 ON
            RB6PPS = 0x0010    'TX1 > RB6
    End SUB
    
     

    Last edit: William Roth 2021-05-25
  • stan cartwright

    stan cartwright - 2021-06-01

    I thought arrays were simple in gcb only they use ram.
    I thought tables were simple in gcb and they are but use prog mem.
    no probs with decent pic

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.