Power of WimBasic

Basic and Machine Language

Moderator: Moderators

Post Reply
wimoos
Vic 20 Afficionado
Posts: 348
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Power of WimBasic

Post by wimoos »

All,

During my efforts to optimize "Chequered flag", I was wondering if some custom characters might already be found in the character ROM.
In such a case I could reuse the ROM-copy, in order to save some RAM-space.

Now, I could use the 'hunt' command in the VICE monitor, but for 87 characters this came over as tedious. Hence, WimBasic to the rescue.

Code: Select all

1 a=varptr(a$):b=varptr(b$):poke a,8:poke b,255:for p=$1600 to $18b0 step 8:doke a+1,p
2 for d=$8000 to $8fff step 248:doke b+1,d:r=1:while 1:s=instr(r,b$,a$):if s else quit
3 if (s-1) and 7 else print hex$(p)"="hex$(d+s-1)
4 r=s+1:wend:next:next
Two string descriptors are set up. One for the bitmaps of the custom character (string of 8 bytes) and one for the bitmaps in ROM to search in (string of maximal 255 bytes). Next, within the FOR/NEXT loops, the string descriptors are filled in.
Then, in a WHILE/WEND loop, INSTR is used for the scan. The result needs to be on 8-byte boundary (check in line 3). When found, a result is printed.

Too bad, only one character found :-(.
WimBasic and documentation can be found at http://wimbasic.webs.com

Regards,

Wim.
Last edited by wimoos on Thu May 07, 2020 12:34 am, edited 1 time in total.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
chysn
Vic 20 Scientist
Posts: 1205
Joined: Tue Oct 22, 2019 12:36 pm
Website: http://www.beigemaze.com
Location: Michigan, USA
Occupation: Software Dev Manager

Re: Power of WimBasic

Post by chysn »

What is WimBasic?
VIC-20 Projects: wAx Assembler, TRBo: Turtle RescueBot, Helix Colony, Sub Med, Trolley Problem, Dungeon of Dance, ZEPTOPOLIS, MIDI KERNAL, The Archivist, Ed for Prophet-5

WIP: MIDIcast BASIC extension

he/him/his
User avatar
R'zo
Vic 20 Nerd
Posts: 514
Joined: Fri Jan 16, 2015 11:48 pm

Re: Power of WimBasic

Post by R'zo »

chysn wrote: Wed May 06, 2020 11:15 am What is WimBasic?
It's basic expansion. Look at the mimoos link in the original post.
R'zo
I do not believe in obsolete...
wimoos
Vic 20 Afficionado
Posts: 348
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: Power of WimBasic

Post by wimoos »

In addition to comparing memory blocks, WimBasic also offers a nice technique for copying memory blocks.
This can be done with the Left-MID$ function, in combination with VARPTR. Useful for setting up user-defined graphics!
Restriction: the target needs to be on a higher address than the programtext.

Like so:

Code: Select all

1 s=varptr(s$):t=varptr(t$):poke s,255:poke t,255
2 doke s+1,$8000:doke t+1,$1800:mid$(t$,1)=s$
3 doke s+1,$80ff:doke t+1,$18ff:mid$(t$,1)=s$
4 doke $19fe,deek($81fe)
5 doke s+1,deek($2d)-696:doke t+1,$1a00:mid$(t$,1)=s$
6 doke s+1,deek($2d)-441:doke t+1,$1aff:mid$(t$,1)=s$
7 doke s+1,deek($2d)-186:doke t+1,$1bfe:mid$(t$,1,186)=s$
Regards,

Wim.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
wimoos
Vic 20 Afficionado
Posts: 348
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: Power of WimBasic

Post by wimoos »

Copying memory blocks this way may also be used to manipulate arrays.
- Swapping the contents of two arrays (using a string as the intermediate buffer).
- (Re-)Initializing an array (e.g. with the contents of a string of 0's (STRING$(sizeof(array),0)).

When the arraysize is 255 bytes or less, this can be done in one MID$ statement per operation, otherwise use more MID$ statements.
This means an array of 51 float elements, 127 integer elements or 85 stringdescriptor elements.

Make sure you use the VARPTR-function as late as possible before the MID$ statement.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
wimoos
Vic 20 Afficionado
Posts: 348
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: Power of WimBasic

Post by wimoos »

When you program in Basic on a VIC20, often speed and/or memory can be a issue.

One trick to save memory and improve speed is to put as many statements as possible, separated by colons, on one Basic line.
Each Basic line has an overhead of 4 bytes, so each line you save is 4 bytes you save.

When performing a GOTO or GOSUB, the target is found by handling lines. So each line you save potentially makes a faster GOSUB or GOTO.
When you use an IF, there is no escape: another line needs to be used. In that sense, IF's slow down your program.
When a target is searched, first the line after the current is checked. If the target is smaller, then the search starts from the beginning of the program.
So when you implement a short loop using GOTO, at the bottom of your program, this is very inefficient.

What you can do here to avoid the search for linenumbers is fiddling with FOR/NEXT loops. When processing reaches NEXT and the terminating condition is false, the point to where to return to is not found through linenumbers, but through contents on the internal stack.
Also, the use of GOSUB can enable you to put more statements on one Basic line, but this induces another GOSUB linesearch. The RETURN is (like FOR/NEXT) done through context on the internal stack. And, what you sometimes can do use calculations to achieve the required outcome, instead of using IF and GOTO-ing back and forth.

In WimBasic I have implemented WHILE/WEND and QUIT. These statements can be used anywhere in a programline, they may be nested, and they work (like FOR/NEXT) with the internal stack. Also WimBasic offers IF/THEN/ELSE, which results in less (potentially long) searches for linenumbers.
Both can be combined in order to synthesize a REPEAT/UNTIL: WHILE 1:......:IF x QUIT ELSE WEND.
When a THEN clause finishes with a GOTO, then it is possible to take the line following the IF as the ELSE clause (or vice versa), again saving another line.

And WimBasic offers MIN, MAX and XOR. So a line like IF S>H THEN H=S can be replaced with H=MAX(S,H) and no new line needed.
Another fine example that I used (not exactly the same, but functional enough): I replaced IF P<>L THEN T=-T first with IF P-L:T=-T but later with T=XOR(T,P<>L) and no new line needed. Could be done in vanilla Basic with T=T*SGN((P<>L)+.5), or, in this case T=T*((P<>L)+.5), but that takes a few more byte than the WimBasic alternative.

Regards,

Wim.
Last edited by wimoos on Wed Nov 11, 2020 3:25 am, edited 2 times in total.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
Noizer
Vic 20 Devotee
Posts: 297
Joined: Tue May 15, 2018 12:00 pm
Location: Europa

Re: Power of WimBasic

Post by Noizer »

wimoos wrote: Mon Nov 02, 2020 9:34 am When you program in Basic on a VIC20, often speed and/or memory can be a issue.
One trick to save memory and improve speed is to put as many statements as possible, separated by colons, on one Basic line.
Each Basic line has an overhead of 4 bytes, so each line you save is 4 bytes you save...
Yes, I can confirm that. Reaching speed in Basic v2 works as you described. Winbasic don‘t know. Reason why I prefer ML. Speed gain is in another dimension of time 😵
Valid rule today as earlier: 1 Byte = 8 Bits
-._/classes instead of masses\_.-
User avatar
Noizer
Vic 20 Devotee
Posts: 297
Joined: Tue May 15, 2018 12:00 pm
Location: Europa

Re: Power of WimBasic

Post by Noizer »

wimoos wrote: Tue May 05, 2020 11:55 am All,

During my efforts to optimize "Chequered flag", I was wondering if some custom characters might already be found in the character ROM.
In such a case I could reuse the ROM-copy, in order to save some RAM-space.

Now, I could use the 'hunt' command in the VICE monitor, but for 87 characters this came over as tedious. Hence, WimBasic to the rescue.

Code: Select all

1 a=varptr(a$):b=varptr(b$):poke a,8:poke b,255:for p=$1600 to $18b0 step 8:doke a+1,p
2 for d=$8000 to $8fff step 248:doke b+1,d:r=1:while 1:s=instr(r,b$,a$):if s else quit
3 if (s-1) and 7 else print hex$(p)"="hex$(d+s-1)
4 r=s+1:wend:next:next
Two string descriptors are set up. One for the bitmaps of the custom character (string of 8 bytes) and one for the bitmaps in ROM to search in (string of maximal 255 bytes). Next, within the FOR/NEXT loops, the string descriptors are filled in.
Then, in a WHILE/WEND loop, INSTR is used for the scan. The result needs to be on 8-byte boundary (check in line 3). When found, a result is printed.

Too bad, only one character found :-(.
WimBasic and documentation can be found at http://wimbasic.webs.com

Regards,

Wim.
Why searching only on 8 byte boundary? Maybe it could find some more sequences with 1 byte steps?!
Valid rule today as earlier: 1 Byte = 8 Bits
-._/classes instead of masses\_.-
wimoos
Vic 20 Afficionado
Posts: 348
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: Power of WimBasic

Post by wimoos »

Why searching only on 8 byte boundary? Maybe it could find some more sequences with 1 byte steps?!
Maybe I could have found more sequences, but character bitmappings are constrained to 8 byte boundary.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
Post Reply