BBC BASIC for Windows
Programming >> BBC BASIC language >> Help - Problem using two lists of data
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1312480809

Help - Problem using two lists of data
Post by 2 data lists on Aug 4th, 2011, 6:00pm

I wrote two small programmes each using its own data. I then decided to put both programmes together for access using a choice input. Each seperate program and its data are in seperate procs but program always reads data from first data element.
Q1. Can make data local to a proc.
Q2. Can I force the second proc to jump to its first data element.
Re: Help - Problem using two lists of data
Post by admin on Aug 4th, 2011, 9:28pm

on Aug 4th, 2011, 6:00pm, Guest-2 data lists wrote:
Q1. Can make data local to a proc.
Q2. Can I force the second proc to jump to its first data element.

Here's a program which illustrates two procedures each with their own independent DATA. BBC BASIC doesn't have an official PRIVATE DATA statement (although it does have LOCAL DATA) so the code uses the equivalent PRIVATE !376 instead:

Code:
      FOR I% = 1 TO 6
        PROC1
        PROC2
      NEXT I%
      END
      
      DEF PROC1
      PRIVATE !376, initdone%
      IF NOT initdone% THEN
        RESTORE +1
        initdone% = TRUE
      ENDIF
      READ A$
      PRINT A$ ;
      ENDPROC
      DATA The, five, boxing, wizards, jump, quickly.
      
      DEF PROC2
      PRIVATE !376, initdone%
      IF NOT initdone% THEN
        RESTORE +1
        initdone% = TRUE
      ENDIF
      READ A$
      PRINT TAB(30) A$
      ENDPROC
      DATA Jackdaws, love, my, big, sphinx, of quartz. 

Richard.
Re: Help - Problem using two lists of data
Post by 0blivion on Aug 6th, 2011, 3:38pm

Thank you for the reply.

To make my program work I had to cut and paste 5 of your lines which worked well.
But
I don't know what these lines are actaly doing so have not learnt anything.

PRIVATE !376, initdone%
IF NOT initdone% THEN
RESTORE +1
initdone% = TRUE
ENDIF



Re: Help - Problem using two lists of data
Post by admin on Aug 6th, 2011, 5:12pm

on Aug 6th, 2011, 3:38pm, 0blivion wrote:
I don't know what these lines are actaly doing so have not learnt anything.

Apart from the PRIVATE !376, which I explained in my previous reply, they are standard BBC BASIC statements so to find out "what they are actually doing" just means reading the Help documentation! I have commented them below:

Code:
      PRIVATE !376, initdone% : REM Make the data pointer private, and ...
      REM ... create a private Boolean to flag that the procedure has been called
      IF NOT initdone% THEN
        REM If this is the first time the procedure was called ...
        RESTORE +1 : REM ... set the data pointer to the next DATA statement ...
        initdone% = TRUE : REM ... and flag that the procedure has been called. 
      ENDIF 

Richard.
Re: Help - Problem using two lists of data
Post by DDRM on Aug 8th, 2011, 09:09am

Hi Richard,

Yes, I worked out what must be going on, but I was astonished to see that you could make what looks like a direct memory address private!

Presumably !376 addresses a memory location deep in the innards of BB4W, which contains the data pointer. I can see that you can use it like any other integer variable, but I am amazed that you can make it private. When it meets a PRIVATE statement, does BB4W go to see if it has already created a version of the variable, and if so, restore the version it has hidden away somewhere, while storing the higher-level version on the stack, restoring it when the procedure ends?

I don't NEED this information, but I am intrigued about how direct memory addressing can be used in this way!

Best wishes,

David
Re: Help - Problem using two lists of data
Post by admin on Aug 8th, 2011, 1:56pm

on Aug 8th, 2011, 09:09am, DDRM wrote:
Yes, I worked out what must be going on, but I was astonished to see that you could make what looks like a direct memory address private!

You shouldn't be, and if I may say so it betrays a serious misunderstanding about the way indirection works. It is a fundamental property of the indirection operators ? and ! that they create L-values; if you don't know what an L-value is, check out this Wikipedia article:

http://en.wikipedia.org/wiki/Value_%28computer_science%29

So, since indirection operators create L-values, they may be used whenever an L-value is syntactically valid in BBC BASIC, and of course this includes the parameters of LOCAL and PRIVATE.

The BB4W manual alludes to this in the section 'Query as a byte variable':

http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin2.html#bytequery

There it explains that (even before the introduction of 'true' byte-variables using the & suffix) 8-bit unsigned L-values have always been available in BBC BASIC using the ? indirection operator.

Quote:
I am amazed that you can make it private.

You were astonished, now you are amazed! Nevertheless, it has been true of BBC BASIC since the very beginning on the BBC Micro (there was no PRIVATE then, of course, but it had LOCAL). Perhaps a little boning up on basic Computer Science principles would not go amiss. wink

Richard.
Re: Help - Problem using two lists of data
Post by DDRM on Aug 10th, 2011, 08:29am

Hi Richard,

Thanks for the pointers about how it works... I don't think I can have a "serious misunderstanding" about something I have no understanding of, but a bit of reading may remedy that situation! smiley

I'm sure a bit of boning up on computer science would be very good for me. I don't remember L-values being an important part of computer science A level in 1980, but if it was it has escaped from my mind since - as with so many things...

Best wishes,

D