BBC BASIC
« Fun challenge »

Welcome Guest. Please Login or Register.
Jan 20th, 2018, 6:11pm


Cross-platform BBC BASIC (Win32, Linux x86, Android, Mac OS-X, Raspberry Pi)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Fun challenge  (Read 741 times)
Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
xx Fun challenge
« Thread started on: Oct 28th, 2016, 10:38pm »

Just for a bit of fun, take a look at the code below. I had cause to write it recently, but can you work out what it does? It modifies the string a$ but how, and why would it be useful in the context of BBC BASIC?

Code:
      DIM xVDU&(31)
      xVDU&() = 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,5,0,0,1,9,8,5,0,1,4,4,0,2
      FOR P% = !^a$ TO !^a$ + LENa$ - 1
        IF ?P%<&20 L%=xVDU&(?P%) : O%=P% : P%+=L% : IF ?O%<>18 IF ?O%<>25 MID$(a$,O%-!^a$+1,L%+1) = "??????????"
      NEXT 

Hint: the name of the array should give you a clue and you should recognise that pattern of numbers.

No prizes, other than the satisfaction of feeling smug if you can work it out!

Richard.
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
xx Re: Fun challenge
« Reply #1 on: Oct 30th, 2016, 10:33am »

on Oct 28th, 2016, 10:38pm, Richard Russell wrote:
Just for a bit of fun, take a look at the code below... can you work out what it does?

Does nobody want to stick their head above the parapet? The code is standard documented stuff, nothing tricky or sneaky, but hopefully sufficiently challenging to keep those neurons exercised. I know better than most the truth of the adage 'use it or lose it' (my brain wasn't 'all there' to start with, since I've had a significant chunk missing since birth).

Richard.
User IP Logged

hellomike
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 55
xx Re: Fun challenge
« Reply #2 on: Nov 6th, 2016, 6:28pm »

Ok, millions of my neurons were tortured for close than an hour, stepping through the code using debug, List Variables and the Memory Monitor.
Unfortunately I was unable to figure out what the code does or its purpose.

I wouldn't mind another hint.

Mike
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
xx Re: Fun challenge
« Reply #3 on: Nov 6th, 2016, 7:04pm »

on Nov 6th, 2016, 6:28pm, hellomike wrote:
I wouldn't mind another hint.

You can cheat and look at the answer posted over at the discussion group, unless of course you don't want it made that easy!

Richard.
User IP Logged

hellomike
Junior Member
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 55
xx Re: Fun challenge
« Reply #4 on: Nov 7th, 2016, 11:49am »

Couldn't resist.

OK, so actually it is a snippet and a$ is declared before! Yes then it makes sense. But on its own, a$ is declared when it is first references, after the FOR and its length is zero so the FOR NEXT is executed only once with an empty string.

Guess not being native English, I failed to understand what you meant by "It modifies the string a$". I saw that in theory, when LENa$>0, it would but couldn't understand how an empty string is to be modified this way. :-)

If you would have put it like this:
Code:
      ...
      DIM xVDU&(31)
      xVDU&() = 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,5,0,0,1,9,8,5,0,1,4,4,0,2
      FOR P% = !^a$ TO !^a$ + LENa$ - 1
        IF ?P%<&20 L%=xVDU&(?P%) : O%=P% : P%+=L% : IF ?O%<>18 IF ?O%<>25 MID$(a$,O%-!^a$+1,L%+1) = "??????????"
      NEXT 
      ... 


it would have made more sense (to me).

Next challenge please. :-)
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
xx Re: Fun challenge
« Reply #5 on: Nov 8th, 2016, 9:50pm »

on Nov 7th, 2016, 11:49am, hellomike wrote:
OK, so actually it is a snippet and a$ is declared before! Yes then it makes sense.

I would have hoped it was obvious that the code can't do anything useful in isolation. Indeed, if entered with a zero-length a$ it immediately malfunctions because the P%=!^a$ sets P% to an ill-defined value (you can't meaningfully refer to a 'pointer to an empty string').

If you like you can consider that to be part of the challenge: before analysing what the code does you've got to work out a context in which it makes sense.

Richard.
User IP Logged

Leo
New Member
Image


member is offline

Avatar




PM


Posts: 10
xx Re: Fun challenge
« Reply #6 on: Nov 9th, 2016, 09:21am »

Ok - my first post.
xVDU& contains the number of parameter bytes that follow each VDU command.
String a$ contains binary representations of VDU commands and their parameters (it may contain other things too).
The snippet finds these and replaces the binary VDU command and parameters with '?' strings.
The only exception is GCOL and PLOT (VDU 18 and 25) which are left intact.

User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
xx Re: Fun challenge
« Reply #7 on: Nov 9th, 2016, 1:57pm »

on Nov 9th, 2016, 09:21am, Leo wrote:
The snippet finds these and replaces the binary VDU command and parameters with '?' strings.
The only exception is GCOL and PLOT (VDU 18 and 25) which are left intact.

Indeed, a perfect explanation. You didn't suggest why one might want to do that, but I've posted the reason elsewhere: it was for a 'List Variables' utility in which I wanted to be able to display the contents of arbitrary unknown strings without crashing as a result of one or more containing a 'damaging' VDU sequence.

Allowing the GCOL and PLOT commands to remain intact was a bit of a gimmick so that strings containing graphic 'icons' would display as intended, but I subsequently removed that feature.

The complete List Variables utility is supplied with the latest release of BBCSDL (you can find it as listvars.bbc in the examples/tools directory). It's intended to be run from SDLIDE using the Run... Debug menu selection (or clicking the Beetle button in the toolbar).

For people who like to examine and learn from programs written by others, there's quite a rich selection included with BBCSDL. The two supplied IDEs, related tools and libraries are all BBC BASIC programs, in most cases with little or no embedded assembler code or deliberate obfuscation.

Richard.
« Last Edit: Nov 9th, 2016, 1:57pm by Richard Russell » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Donate $6.99 for 50,000 Ad-Free Pageviews!


This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls