Announcement

Collapse

Maintenance

The forum could be offline for 30-60 minutes in the very near future for maintenance (said 3pm Pacific). I was behind on getting this notice. I do apologize.
See more
See less

Can static arrays be shared ??

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can static arrays be shared ??

    Hi !

    I have no real experience with PB programming, so maybe I'm doing something wrong.. anyway I'm having problems accessing an array (declared as STATIC and SHARED) from a SUB.
    It seems I can't access more than 255 elements.
    Maybe static arrays can't be shared ?? That is, they can't be global ??? The array has 512 elements.
    If I remove the code from the SUB and put it in the global scope, I can access all the elements correctly.
    If so, should I pass the memory pointer of the array to my SUB ?

    Thanks for any help.

    Here it is the source (it prints bitmap fonts on a dot-matrix display connected through parallel port) :

    Code:
    '$COMPILE UNIT
    '$COMPILE EXE
    $CPU 80386
    $LIB LPT OFF
    $LIB GRAPH OFF
    $LIB FULLFLOAT OFF
    $LIB IPRINT OFF
    '$ERROR ALL OFF
    $OPTION CNTLBREAK OFF
    
    DEFINT A-Z
    
    DECLARE SUB DisplayTextOut(StringToDisplay AS STRING)
    DECLARE SUB DoEnablePulse(BYVAL RegisterStatus AS BYTE)
    
    DIM STATIC BUFFER_FONT8x8(511) AS SHARED BYTE
    
    ' SPAZIO
    BUFFER_FONT8x8(0)=0
    BUFFER_FONT8x8(1)=0
    BUFFER_FONT8x8(2)=0
    BUFFER_FONT8x8(3)=0
    BUFFER_FONT8x8(4)=0
    BUFFER_FONT8x8(5)=0
    BUFFER_FONT8x8(6)=0
    BUFFER_FONT8x8(7)=0
    '
    BUFFER_FONT8x8(488)=0
    BUFFER_FONT8x8(489)=0
    BUFFER_FONT8x8(490)=65
    BUFFER_FONT8x8(491)=65
    BUFFER_FONT8x8(492)=127
    BUFFER_FONT8x8(493)=127
    BUFFER_FONT8x8(494)=0
    BUFFER_FONT8x8(495)=0
    '
    BUFFER_FONT8x8(496)=0
    BUFFER_FONT8x8(497)=8
    BUFFER_FONT8x8(498)=12
    BUFFER_FONT8x8(499)=254
    BUFFER_FONT8x8(500)=254
    BUFFER_FONT8x8(501)=12
    BUFFER_FONT8x8(502)=8
    BUFFER_FONT8x8(503)=0
    '
    BUFFER_FONT8x8(504)=0
    BUFFER_FONT8x8(505)=24
    BUFFER_FONT8x8(506)=60
    BUFFER_FONT8x8(507)=126
    BUFFER_FONT8x8(508)=24
    BUFFER_FONT8x8(509)=24
    BUFFER_FONT8x8(510)=24
    BUFFER_FONT8x8(511)=24
    
    ' Display OFF
    'OUT &H37A,7
    'OUT &H378,&H3E
    'CALL DoEnablePulse(7)
    
    ' Set the Initial Display Line
    'OUT &H378,&HC0
    'CALL DoEnablePulse(7)
    
    ' Set Page Address
    'OUT &H378,&HB8
    'CALL DoEnablePulse(7)
    
    ' Set Y Address
    'OUT &H378,&H40
    'CALL DoEnablePulse(7)
    
    CALL DisplayTextOut("A")
    
    ' Display ON
    'OUT &H37A,7
    'OUT &H378,&H3F
    'CALL DoEnablePulse(7)
    
    SUB DisplayTextOut(StringToDisplay AS STRING)
     DIM i AS LOCAL BYTE
     DIM y AS LOCAL BYTE
     DIM Lenght AS LOCAL BYTE
     DIM Offset_Start AS LOCAL WORD
     DIM Offset_End AS LOCAL WORD
    
     Length=LEN(StringToDisplay)
     IF Length<17 THEN
    	'OUT &H37A,6
    	FOR i=1 to Length
    		'y=ASC(StringToDisplay,i)
                    'PRINT y
    		Offset_Start=ASC(StringToDisplay,i)-32
    		SHIFT LEFT Offset_Start,3
            	Offset_End=Offset_Start+7
                    PRINT Offset_Start
            	' Write BitMap Data
            	FOR y=Offset_Start TO Offset_End
    	                PRINT BUFFER_FONT8x8(y)
    			'OUT &H378,BUFFER_FONT8x8(y)
     			'CALL DoEnablePulse(6)
    		NEXT
     	NEXT
     END IF
    END SUB
    
    SUB DoEnablePulse(BYVAL RegisterStatus AS BYTE)
     DIM i AS LOCAL BYTE
     ' E=0
     OUT &H37A,RegisterStatus-4
     FOR i=0 to 1
     NEXT i
     ' E=1
     OUT &H37A,RegisterStatus
    END SUB


    [This message has been edited by Franco Lazzarotti (edited August 19, 2006).]

  • #2
    Franco,
    there is no point making an array STATIC and SHARED at the same time. Just make it SHARED.

    Your code produces an overflow error on the line:
    Code:
       FOR y=Offset_Start TO Offset_End
    The error is because y is define as a BYTE but it's being allocated a value of 264 which is beyond the range of a BYTE.

    Paul.

    ------------------

    Comment


    • #3
      Or pass the entire array as an additional parameter to using subs...

      Code:
      Main:
         STATIC  Z() AS LONG
        ....
         ....
         CALL myfunction (A,b, c, Z())
         ....
      
          END
      
      FUNCTION MyFunction (A AS integer, b as integer, c as integer, Z() AS LONG)
      
       ' you can do anything you want with Z() in this function
      
      END FUNCTION
      ...

      Michael Mattias
      Tal Systems Inc. (retired)
      Racine WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        I have no real experience with PB programming
        Just as a programming tip, you can save yourself a heck of a
        lot of typing by using Data statements. Example for your
        program:
        Code:
        data 0,  0,  0,  0,  0,  0,  0,  0
        data 0,  0,  0, 79, 79,  0,  0,  0
        for x = 1 to finish
        t$ = read$
        BUFFER_FONT8x8(x) = val(t$)
        next x
        It's been a while since I used data statements in a DOS program
        so the syntax may be a little off, but it should give you the
        idea.


        ------------------


        [This message has been edited by Mel Bishop (edited August 01, 2006).]
        There are no atheists in a fox hole or the morning of a math test.
        If my flag offends you, I'll help you pack.

        Comment


        • #5
          [quote]Originally posted by Mel Bishop:
          It's been a while since I used data statements in a DOS program
          so the syntax may be a little off, but it should give you the
          idea.

          DATA 0, 0, 0, 0, 0, 0, 0, 0:' spazio
          DATA 0, 0, 0, 79, 79, 0, 0, 0:' !
          DATA 0, 7, 7, 0, 0, 7, 7, 0:' "
          DATA 20,127,127, 20, 20,127,127, 20:' #
          DATA 0, 36, 46,107,107, 58, 18, 0:' $
          DATA 0, 99, 51, 24, 12,102, 99, 0:' %
          DATA 0, 50,127, 77, 77,119,114, 80:' &
          DATA 0, 0, 0, 4, 6, 3, 1, 0:' '
          DATA 0, 0, 28, 62, 99, 65, 0, 0:' (
          DATA 0, 0, 65, 99, 62, 28, 0, 0:' )
          DATA 8, 42, 62, 28, 28, 62, 42, 8:' *
          DATA 0, 8, 8, 62, 62, 8, 8, 0:' +
          DATA 0, 0,128,224, 96, 0, 0, 0:' ,
          DATA 0, 8, 8, 8, 8, 8, 8, 0:' -
          DATA 0, 0, 0, 96, 96, 0, 0, 0:' .
          DATA 0, 64, 96, 48, 24, 12, 6, 2:' /
          DATA 0, 62,127, 73, 69,127, 62, 0:' 0
          DATA 0, 64, 68,127,127, 64, 64, 0:' 1
          DATA 0, 98,115, 81, 73, 79, 70, 0:' 2
          DATA 0, 34, 99, 73, 73,127, 54, 0:' 3
          DATA 0, 24, 24, 20, 22,127,127, 16:' 4
          DATA 0, 39,103, 69, 69,125, 57, 0:' 5
          DATA 0, 62,127, 73, 73,123, 50, 0:' 6
          DATA 0, 3, 3,121,125, 7, 3, 0:' 7
          DATA 0, 54,127, 73, 73,127, 54, 0:' 8
          DATA 0, 38,111, 73, 73,127, 62, 0:' 9
          DATA 0, 0, 0, 36, 36, 0, 0, 0:' :
          DATA 0, 0,128,228,100, 0, 0, 0:' ;
          DATA 0, 8, 28, 54, 99, 65, 65, 0:' <
          DATA 0, 20, 20, 20, 20, 20, 20, 0:' =
          DATA 0, 65, 65, 99, 54, 28, 8, 0:' >
          DATA 0, 2, 3, 81, 89, 15, 6, 0:' ?
          DATA 0, 62,127, 65, 77, 79, 46, 0:' @
          DATA 0,124,126, 11, 11,126,124, 0:' A
          DATA 0,127,127, 73, 73,127, 54, 0:' B
          DATA 0, 62,127, 65, 65, 99, 34, 0:' C
          DATA 0,127,127, 65, 99, 62, 28, 0:' D
          DATA 0,127,127, 73, 73, 65, 65, 0:' E
          DATA 0, 95,127, 9, 9, 1, 1, 0:' F
          DATA 0, 62,127, 65, 73,123, 58, 0:' G
          DATA 0,127,127, 8, 8,127,127, 0:' H
          DATA 0, 0, 65,127,127, 65, 0, 0:' I
          DATA 0, 32, 96, 65,127, 63, 1, 0:' J
          DATA 0,127,127, 28, 54, 99, 65, 0:' K
          DATA 0,127,127, 64, 64, 64, 64, 0:' L
          DATA 0,127,127, 6, 12, 6,127,127:' M
          DATA 0,127,127, 14, 28,127,127, 0:' N
          DATA 0, 62,127, 65, 65,127, 62, 0:' O
          DATA 0,127,127, 9, 9, 15, 6, 0:' P
          DATA 0, 30, 63, 33, 97,127, 94, 0:' Q
          DATA 0,127,127, 25, 57,111, 70, 0:' R
          DATA 0, 38,111, 73, 73,123, 50, 0:' S
          DATA 0, 1, 1,127,127, 1, 1, 0:' T
          DATA 0, 63,127, 64, 64,127, 63, 0:' U
          DATA 0, 31, 63, 96, 96, 63, 31, 0:' V
          DATA 0,127,127, 48, 24, 48,127,127:' W
          DATA 0, 99,119, 28, 28,119, 99, 0:' X
          DATA 0, 7, 15,120,120, 15, 7, 0:' Y
          DATA 0, 97,113, 89, 77, 71, 67, 0:' Z
          DATA 0, 0,127,127, 65, 65, 0, 0:'
          DATA 0, 62,127,127,127,127, 62, 0:'
          DATA 0, 0, 65, 65,127,127, 0, 0:'
          DATA 0, 8, 12,254,254, 12, 8, 0:'
          DATA 0, 24, 60,126, 24, 24, 24, 24:'

          DIM Buffer_Font8x8(511) AS SHARED BYTE
          DIM Counter AS INTEGER

          FOR Counter = 0 TO 511
          READ Buffer_Font8x8(Counter)
          PRINT Buffer_Font8x8(Counter);
          NEXT Counter

          END

          ------------------
          "The trouble with quotes on the Internet is that you can never know if they are genuine." - Abraham Lincoln.

          Comment


          • #6
            Thank alls for suggestions and help. Actually the problem was a wrong variable declaration. After that, it works fine. I'm thinking of passing the pointer, maybe it could be a little faster than accessing a global buffer ? As regards the using of 'DATA' statements, that series of 'BUFFER_FONT8x8[n]=d' was created automatically by a program I made to convert bitmap fonts, so that's the best choice. Thank you.



            [This message has been edited by Franco Lazzarotti (edited August 19, 2006).]

            Comment


            • #7
              >>I'm thinking of passing the pointer,
              >>maybe it could be a little faster than accessing a global buffer

              ??

              Passing a pointer is exactly the same thing as passing the data item itself by reference (the default).

              As far as 'faster' there are so many other variables involved the only true test will be... an actual test.

              But in general, tinkering with passing a pointer versus the variable itself it will make no measurable leave alone noticeable difference.

              But if a variable is global, why pass it at all? That just says "I made this variable GLOBAL (well, SHARED in PB/DOS-speak) for no good reason."

              Michael Mattias
              Tal Systems Inc. (retired)
              Racine WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                'As Michael said, global isn't needed.
                'In PB/WIN PB/CC, anyway, arrays are passed by BYREF very fast
                Code:
                #COMPILE EXE
                #DIM ALL
                FUNCTION PBMAIN () AS LONG
                  LOCAL x() AS LONG
                  LOCAL y AS LONG
                  DIM  x(99)
                  FOR y = 1 TO 10000000
                    CALL test1(x())     'only address passed
                  NEXT
                  PRINT x(0), x(99)
                  SLEEP 1000
                  'result 10000000, -10000000
                END FUNCTION
                
                SUB Test1(BYREF x()AS LONG) 'BYREF is default
                  INCR x(0)
                  DECR  x(99)
                END SUB
                ------------------
                How long is an idea? Write it down.

                Comment

                Working...
                X