Challenge: Create or Delete Lines from inside a program

Basic and Machine Language

Moderator: Moderators

User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Challenge: Create or Delete Lines from inside a program

Post by Jeff-20 »

I had a small program challenge. I want to share it to see alternate solutions. The challenge is to make a program create or delete a line of code. The program could exit to BASIC to complete the task. My solution was very crude but effective. To erase lines, I just had the program determine the lines and POKE line numbers into the keyboard buffer followed by returns and exit the program. I bet there are more elegant solutions.
High Scores, Links, and Jeff's Basic Games page.
Boray
Musical Smurf
Posts: 4064
Joined: Mon May 03, 2004 10:47 am

Re: Challenge: Create or Delete Lines from inside a program

Post by Boray »

That is about the same thing I did here:
http://www.boray.se/commodore/datamaker.html
PRG Starter - a VICE helper / Vic Software (Boray Gammon, SD2IEC music player, Vic Disk Menu, Tribbles, Mega Omega, How Many 8K etc.)
FD22
Vic 20 Hobbyist
Posts: 148
Joined: Mon Feb 15, 2010 12:31 pm

Re: Challenge: Create or Delete Lines from inside a program

Post by FD22 »

The internal memory structure of BASIC program storage is pretty straight-forward - hit TVR or PRG for details. There are a slew of ROM routines for adding, inserting and removing lines (and taking care of the appropriate pointers) which the screen editor calls to make things happen and which you could call if you didn't want to re-invent the wheel.

I'm wondering why you'd want to do this from BASIC itself though - self-modifying code can be a useful technique sometimes, but in BASIC you're probably going to write more code to do the self-modifying than you'd need to just write as conditional logic in the first place.
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Challenge: Create or Delete Lines from inside a program

Post by Mike »

Jeff,

for what reason would you want to delete, add, or possibly also modify lines from within a program, besides the odd 'insert expression for a function plotter', 'create a data loader', or 'tokenize a PETSCII SEQ file'?

We had a similar discussion about this some time ago, see 'Reflection in BASIC program'. In short, besides some of the applications given above, self-modifying code does not lead to capabilities which couldn't be expressed with static code alone.

It's 'easy' enough to come up with some alternate solution which wouldn't use the PRINT-and-keyboard-buffer method, but shuffles around the BASIC program in memory. Normally this would require to clear all variables. Keeping also the variables alive is doable, but an entirely other ballpark! In any case, machine code is the weapon of choice, which I know you wouldn't use anyway. :(
wimoos
Vic 20 Afficionado
Posts: 345
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: Challenge: Create or Delete Lines from inside a program

Post by wimoos »

Jeff,

WimBasic allows you to CHAIN a program to another (EXEC "<up-arrow>NEXTPRG"). Also, SuperNumbers variables retain their value from one program to another.
Further, there's also the DEL command, to delete a line or range of lines, and MERGE to merge programs together (the latter only from datasette, so I wouldn't advise that in this case).

Regards,

Wim.
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Re: Challenge: Create or Delete Lines from inside a program

Post by Jeff-20 »

I am always interested in programming challenges within restrictions like Basic.

Mike,
Sorry for being repetitive (it's a common problem with me). I started this thread after coming across the "reflection" thread in a search. It's for a program I was working on at that time and recently revisited. The program would be a collection of BASIC line I commonly use (joystick routines and such). I would select lines/assets from a menu and delete everything else.

Wim,
I'll check it out However, for exercise, I am determined to make this work in stock basic.
High Scores, Links, and Jeff's Basic Games page.
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Challenge: Create or Delete Lines from inside a program

Post by Mike »

Jeff-20 wrote:The program would be a collection of BASIC line[s] I commonly use (joystick routines and such).
That's an interesting application for itself. I'm assuming that this is also supposed to run on the real iron. :)

Of course you could keep the different code snippets as single files on a disk (or disk image, for that matter). Then you only would have to link such a file at the back end without deleting the current program - this also allows to change the line number(s).
I am determined to make this work in stock basic.
It's a bit complicated to do this with BASIC alone, but you'd proceed as follows:

1.) Save away the current BASIC start:

Code: Select all

POKE3,PEEK(43):POKE4,PEEK(44)
2.) Raise the BASIC start to the end of the current program:

Code: Select all

A=PEEK(45)+256*PEEK(46)-2:POKE44,A/256:POKE43,A-256*PEEK(44):CLR
3.) LOAD the code snippet

Code: Select all

LOAD"JOYSTICK",8
... or whatever else.

4.) Change all lines number as appropriate. They must be higher than the line numbers of the preceding program, otherwise this leads to trouble with GOTO and GOSUB. Don't forget to delete all lines with the old line numbers.

5.) Restore the old BASIC start:

Code: Select all

POKE43,PEEK(3):POKE44,PEEK(4):CLR
That's the inverse to step 1.

When you now LIST the complete program, you'll see the lines of the code snippet nicely attached to the end of the program.

Greetings,

Michael

P.S. Of course, with tools like BasEdit.net, you won't have to go into these things at all, as a simple Ctrl-C, Ctrl-V from another open text document with those code snippets would serve the same purpose, but I'm going to suppose that wasn't the point. ;)

P.P.S. Woohoo! 2000th post! :D
User avatar
Richardc64
Vic 20 Drifter
Posts: 33
Joined: Mon Feb 01, 2010 3:55 pm

Re: Challenge: Create or Delete Lines from inside a program

Post by Richardc64 »

Mike wrote:Jeff,

for what reason would you want to delete, add, or possibly also modify lines from within a program,
How about after filling a largish array with initial values from DATA statements, deleting the DATA lines and the FOR-READ-NEXT so there'd be more memory available for other variables. Could be useful on an unexpanded Vic.

Just a thought.
"I am endeavoring, ma'am, to create a mnemonic memory circuit... using stone knives and bearskins." -- Spock to Edith Keeler
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Challenge: Create or Delete Lines from inside a program

Post by Mike »

Richardc64 wrote:How about [...]
The 'canonical' solution for this kind of memory management is a two-filer: The first part POKEs user-defined characters, machine code, and other data into memory, perhaps displays a title screen and instructions, and then the main program is being loaded and run.

Freeing memory by deleting lines in-situ makes such a program effectively uneditable. Especially during development, you wouldn't want to reload the program each time it was run before to return it to a 'virgin' state.

In a similar fashion, Jeff actually uses a method, where he single-files his games by including the already POKEd character data into the file. The first instructions in his programs then correct the relevant pointers of BASIC (especially, start of variables and top of available memory), so the program can run normally. However, this method can also only be applied to programs 'after the fact', when they're finished - during development, when the programs are saved to tape/disk, it is necessary to adjust the BASIC pointers the other way, so the character data is not inadvertantly lost.
groepaz
Vic 20 Scientist
Posts: 1180
Joined: Wed Aug 25, 2010 5:30 pm

Re: Challenge: Create or Delete Lines from inside a program

Post by groepaz »

Mike wrote:Especially during development, you wouldn't want to reload the program each time it was run before to return it to a 'virgin' state.
thats one reason that makes crossdevelopment so attractive - you can easily and painlessly do things that require to re-run from a clean state every time. its quite a common thing to do in demo code :)
I'm just a Software Guy who has no Idea how the Hardware works. Don't listen to me.
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Re: Challenge: Create or Delete Lines from inside a program

Post by Jeff-20 »

I so often scavenge my old programs for routines that I tend to keep the variable assignments the same in all of my work. I got tired of always loading old programs to save time. The template would have a simple KEEP/DELETE menu with all of those common lines. Really, it's not much easier than just manually deleting from the line editor. The idea grew into a challenge. That's where the idea of adding lines came; although I cannot imagine how that would be useful to me.
High Scores, Links, and Jeff's Basic Games page.
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Re: Challenge: Create or Delete Lines from inside a program

Post by Jeff-20 »

Problem. The keyboard buffer is pretty small (631-640 in memory). My proposal would only be good for deleting 9 lines.


Sent from my iPhone using Tapatalk
High Scores, Links, and Jeff's Basic Games page.
wimoos
Vic 20 Afficionado
Posts: 345
Joined: Tue Apr 14, 2009 8:15 am
Website: http://wimbasic.webs.com
Location: Netherlands
Occupation: farmer

Re: Challenge: Create or Delete Lines from inside a program

Post by wimoos »

Back in the 80's I had a Basic program that you could use to renumber a "work in progress".

So your own program was somewhere in the 10-1200 range, and the RENUMBER logic went somewhere in the 60000 range. Line 60000 was a REM followed by 80 colons. Whenever you had numbering problems you just did a RUN60000 and your program would be renumbered.

I had this program in text-format on datasette (created with OPEN...CMD...LIST...PRINT..CLOSE), followed by a text-file with only linenumbers 60000, 60010, ... etcetera in it (created with OPEN...FOR...PRINT...NEXT..CLOSE). Next I had a small ML routine that opened a textfile and rerouted it to CHRIN, line-by-line. So, mergeing the renumbering logic went like this, as well as deleting that part if you didn't need it anymore. It's maybe a poor-man solution but hey, it worked like a charm!

Regards,

Wim
VICE; selfwritten 65asmgen; tasm; maintainer of WimBasic
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Challenge: Create or Delete Lines from inside a program

Post by Mike »

Jeff-20 wrote:Problem. The keyboard buffer is pretty small (631-640 in memory). My proposal would only be good for deleting 9 lines.

Code: Select all

63998 INPUT"DELETE LINE";S:INPUT"UP TO";E
63999 IFS<=ET{SHIFT-H}?"{CLR}"S:?"S="S+1"{LEFT}:E="E"{LEFT}:G{SHIFT-O}63999":P{SHIFT-O}631,19:P{SHIFT-O}632,13:P{SHIFT-O}633,13:P{SHIFT-O}198,3
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Re: Challenge: Create or Delete Lines from inside a program

Post by Jeff-20 »

OK, that is a clever solution! Not only does it work, but it opens so many doors for me with a simple epiphany: GOTO does not clear variables (like RUN). It should be obvious, but it never occurred to me before now. Thanks, Mike!

Wim, do you recall your strategy for renumbering in basic? Did you ever change the order of lines?
High Scores, Links, and Jeff's Basic Games page.
Post Reply