Negating the length of string variables.

SYS 64738
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Negating the length of string variables.

Post by Shaun_B »

Here's the first thing that I thought up for working around the limitations of the string length in BASIC (V2) - the example is a scrolly text at the bottom of the screen (written with CBM prg studio, so it should just copy and paste across):

Code: Select all

0 poke 53265,peek(53265) and 239:poke 53280,6:poke 53281,6:print"{clear}";
10 print ".this is the first example of negating the problem with the string";
20 print" length in basic v2, which although it works, it does present the";
30 print" programmer with problems, which are:   ";
40 print"[1] that it's slow, therefore i've switched off the screen whilst";
50 print" this text is being copied from the screen to memory @ 49152, and";
60 print "   [2] essentially you have the data copied twice, once";
70 print" printed to the screen and once copied to the area of memory mentioned";
80 print" above. "
90 print"a more memory- and speed-efficient method is mentioned below...*";
100 sc=1024:m=49152
110 poke m,peek(sc):if peek(sc)<>42 then sc=sc+1:m=m+1:goto 110
120 print"{clear}":poke 53265,peek(53265)or 16
130 m=49152:i=peek(m)
140 m=m+1:if peek(m)<>42 then poke m-1,peek(m):goto 140
150 poke m-1,i:for i=0 to 39:poke 1984+i,peek(49152+i):next:goto 130
Because it's so slow, I've switched off the screen whilst the screen memory is copied to some free memory at 49152; essentially the bytes are shuffled in RAM up to the end of the text (the * at the end is the condition it is looking for): essentially, the first byte (or character) is stored in the temporary variable i, the memory location is increased by one, and the bytes at that location is written to the one before it. When the end of text is reached, the byte in i is stored at the byte before the end of text marker and then the first 40 characters of the text are written byte-by-byte to the bottom line of the screen and it loops back to shuffle the characters again.

Once you run it, you'll see what I mean about the speed, and some help is needed, i.e., SuperCPU or other accelerator.

Each string variable may only be 255 characters in length, but an array may have one or more elements, so if you had a string array of four (dim a$(3) as zero would be counted as one element), then you'd have 4 x 255, or 1020 characters. As the screen may contain up to 1000 characters, this should be more than enough for our purposes. Also, you don't have the problem with repeating data with the above example. Here the listing for you to try with CBM prg studio:

Code: Select all

0 dim a$(3):poke 53280,0:poke 53281,0:print"{clear}"
10 a$(0)=".          this is an example of a getting around the problem of 255"
11 a$(0)=a$(0)+" characters per string: think of each array as a single string"
12 a$(0)=a$(0)+", so a$ is broken into four elements (or fewer as required) a"
13 a$(0)=a$(0)+"s we know that there are 1000 characters by default on a"
14 a$(0)=a$(0)+" commod"
15 a$(1)="ore 64 screen, so dim a$(3) will suffice. if one was to use a 2-"
16 a$(1)=a$(1)+"dimensional array using the same logic, like dim a$(9,3), you"
17 a$(1)=a$(1)+" could say that the first identifier (0 to 9) could state "
18 a$(1)=a$(1)+"the page or location, with the second (0 - 3 inclusive) "
19 a$(1)=a$(1)+"being the conten"
20 a$(2)="ts of the page or location. rather handy if you wanted to try your"
21 a$(2)=a$(2)+" hand at a text adventure, something which i may do. as"
22 a$(2)=a$(2)+" this is just an example, i've already run out of things"
23 a$(2)=a$(2)+" to say and i'm not even 800 characters in yet, so i may "
24 a$(2)=a$(2)+"as well send some gre"
25 a$(3)="etings out, as is customary in scrollys......... greetings to nigel"
26 a$(3)=a$(3)+" p, allan b, bert, wayne w......... happy programming."
27 a$(3)=a$(3)+"......."
30 a$=left$(a$(0),1)
40 for i=0 to 2
45 l=len(a$(i))-1:a$(i)=right$(a$(i),l)+left$(a$(i+1),1)
50 next
55 l=len(a$(3))-1:a$(3)=right$(a$(3),l)+a$
60 print"{home}{right}{dark gray}"mid$(a$(0),1,2)"{gray}"mid$(a$(0),3,1)"{light gray}";
65 print mid$(a$(0),4,1)"{white}"mid$(a$(0),5,30)"{light gray}"mid$(a$(0),35,1)"{gray}"mid$(a$(0),36,1)"{dark gray}"mid$(a$(0),37,2)
70 wait 53265,128,128:get a$:if a$="" then 30
Explanations on request ;-)

Regards,

Shaun.


BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.

Who is online

Users browsing this forum: No registered users and 10 guests