Reflection in a BASIC program

Basic and Machine Language

Moderator: Moderators

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

Reflection in a BASIC program

Post by Jeff-20 »

I've been experimenting with the concept of reflection in my BASIC games (from wikipedia: reflection is the process by which a computer program can observe and modify its own structure and behaviour).

The program would rewrite/modify itself while the game is in process. Just for the sport of it.

The problem is that the program cannot revert to original. So, I have to rethink the whole structure of a game. Could be interesting.
High Scores, Links, and Jeff's Basic Games page.
User avatar
eslapion
ultimate expander
Posts: 5458
Joined: Fri Jun 23, 2006 7:50 pm
Location: Canada
Occupation: 8bit addict

Post by eslapion »

Correct me if I am wrong BUT...

A self modifying ML program is something I have often seen and done in the past, but a self modifying BASIC programs is something I would instinctively expect to be incredibly difficult.

That's because BASIC is stored in memory in the form of tokens. In order to change these tokens, you have to know exactly at what address they are stored. This can instantly change if you cange only one thing in one line of code...

This would kind of make your program completely uneditable.
Be normal.
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Post by Jeff-20 »

That's the point. My programs as they are now cannot be edited after I finish. The exact memory locations are important and would crash the program if changed. So I may as well play around with switching tokens and values within the code itself. :)
High Scores, Links, and Jeff's Basic Games page.
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

One thing you can do is to replace variables with self-modifying code:

10 B=0:POKE 36879,8
20 POKE PEEK(648)*256+B,0
30 B=B+1:IF B<505 THEN 20

10 POKE 36879,8
15 C$=STR$(PEEK(648)*256):FOR A=2 TO LEN(C$):POKE 4168+A,ASC(MID$(C$,A,1)):NEXT
20 POKE 1000,0
22 C=1:FOR A=3 TO 0 STEP -1:B=PEEK(4170+A)+C:C=0
23 IF B>57 THEN B=48:C=1
24 POKE 4170+A,B:NEXT
25 IF C=0 THEN 20

The second program obviously is tailored for an unexpanded VIC-20 although the use of PEEK(648) is made to make it run in any configuration. Line 25 could be improved, it will keep filling the memory until address 9999, far after the screen matrix has ended. It is also quite dog slow compared to the first program.

But yes, it is self-modifying. :lol:
Anders Carlsson

Image Image Image Image Image
6502dude
megacart
Posts: 1581
Joined: Wed Dec 01, 2004 9:53 am

Post by 6502dude »

eslapion wrote:Correct me if I am wrong BUT...
Your ignorance is showing again :oops: :oops:
Last edited by 6502dude on Tue Nov 11, 2008 7:45 am, edited 1 time in total.
Image Mega-Cart: the ultimate cartridge for your Commodore Vic-20
User avatar
eslapion
ultimate expander
Posts: 5458
Joined: Fri Jun 23, 2006 7:50 pm
Location: Canada
Occupation: 8bit addict

Post by eslapion »

6502dude wrote:Your ignorance is showing again :oops: :oops:
Guilty as charged and proud to be! :D

You, on the other hand know everything and therefore have nothing to learn from anyone.

I am ON MY KNEES... :lol: :lol: :lol:
Be normal.
User avatar
eslapion
ultimate expander
Posts: 5458
Joined: Fri Jun 23, 2006 7:50 pm
Location: Canada
Occupation: 8bit addict

Post by eslapion »

Jeff-20 wrote:That's the point. My programs as they are now cannot be edited after I finish. The exact memory locations are important and would crash the program if changed. So I may as well play around with switching tokens and values within the code itself. :)
Evil genius!
:twisted: :wink:
Be normal.
User avatar
pitcalco
just pitcalco
Posts: 1272
Joined: Wed Dec 28, 2005 4:13 pm
Location: Berlin

Post by pitcalco »

6502dude wrote:
eslapion wrote:Correct me if I am wrong BUT...
Your ignorance is showing again :oops: :oops:

Better stick to your hardware skills, such as pluging wall warts into extension cords, sealing the works in a box, and calling it a heavy duty power supply. :roll:

NOOOOOOOOOOOOOOOOO! PLEEEEEEEEEEEEASE! Not another pissing match!

Anyway, Jeff. Speaking as someone who is largely ignorant of the inner workings of such things on the Vic, I am merely wondering what purpose a self-modifying program has in general, or are you simply trying to prove a principle?
There are only three kinds of people in the world: those who can count and those who can't.

Paul Lambert
Berlin
Federal Republic of Germany
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Post by Jeff-20 »

My theory is a program can be made more memory efficient. Just as variables can allow a routine to be reused for different purposes, I could change tokens/commands at different points to make a routine more versatile.

I haven't come up with an exact example yet.
High Scores, Links, and Jeff's Basic Games page.
PaulQ
undead vic
Posts: 1967
Joined: Sun Jan 14, 2007 2:57 pm

Post by PaulQ »

Jeff, here's an article demonstrating a self-modifying program in BASIC on the Commodore PET:

http://www.atarimagazines.com/compute/i ... _BASIC.php

Hope that helps!
CrAlt
Vic 20 Drifter
Posts: 35
Joined: Mon Oct 27, 2008 4:06 pm

Re: Reflection in a BASIC program

Post by CrAlt »

Jeff-20 wrote:
The program would rewrite/modify itself while the game is in process. Just for the sport of it.
For the love of God, Careful with that! I saw a movie once about a self modifying computer program and it didnt turn out well.
The Terminator: The system goes on-line Nov 15th, 2008. Human decisions are removed from strategic defense. VIC-20 begins to learn at a geometric rate. It becomes self-aware at 2:14 a.m. Eastern time, Nov 29th. In a panic, they try to pull the plug.

Sarah Connor: VIC-20 fights back.

The Terminator: Yes. It launches its missiles against the targets in Russia.

John Connor: Why attack Russia? Aren't they our friends now?

The Terminator: Because VIC-20 knows the Russian counter-attack will eliminate its enemies over here.
:lol:
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Post by Mike »

@Jeff

There are only few valid (i.e. sensible) reasons for self-modifying code, some examples:

1. In BASIC I've written a tokenizer that (re-)builds a BASIC program from a SEQ file:

http://sleepingelephant.com/ipw-web/bul ... 6&start=14

2. In a similar way, a program creating a table of function values could provide a formula entry:

Code: Select all

1 INPUT"F(X)=";F$
2 PRINT"{CLR}8 Y=";F$
3 PRINT"GOTO4":POKE631,19:POKE632,13:POKE633,13:POKE198,3:END
4 INPUT"X MIN";X0
5 INPUT"X MAX";X1
6 INPUT"STEP";S
7 FORX=X0TOX1STEPS
8 Y=X:REM this line is modified
9 PRINT X,Y
10 NEXT
11 END
3. In machine language, routines for graphics primitives - such as line drawing routines - quite often use self modifying code. In the case, certain instructions in the inner loop are altered, where otherwise a complete copy of that loop would be necessary.

A line routine I have written places DEY inside such a loop, 8 times, when the line has decrementing Y-coordinates, INY otherwise. For setting points: ORA #$80, ORA #$40, ..., ORA #$01 are placed at the right positions - for clearing points AND #$7F, AND #$BF, ..., AND #$FE.

The routine is roughly 1K long (and plots ~30000 pixels per second), if it were written with static code, it might probably exceed 4K.


Finally, self-modifying code does not lead to expanded capabilities of the code. Everything programmable can be expressed in static code. And what you said here:
Jeff20 wrote:[...] My programs as they are now cannot be edited after I finish. The exact memory locations are important and would crash the program if changed. [...]
is a strong claim, which - unfortunately - you won't be able to defend.

Michael
carlsson
Class of '6502
Posts: 5516
Joined: Wed Mar 10, 2004 1:41 am

Post by carlsson »

Maybe we can coach Jeff into more machine language programming, one step at a time? :-D At least some hybrid programming like Mike helped Orion with in VICtoria Gold.
Anders Carlsson

Image Image Image Image Image
User avatar
Schema
factor
Posts: 1430
Joined: Tue Mar 23, 2004 7:07 am
Website: http://www.jammingsignal.com
Location: Toronto, Ontario

Post by Schema »

Another approach would be to stuff the keyboard buffer with the code changes you want to make followed by RUN or GOTO ###, then exit the program with END. You'd lose your variables this way, so you'd have to POKE memory locations instead to store values.

Reflection is built into Java, I've dabbled in it a little bit. It's possible to make your program an incredibly confusing mess though, so it should be used sparingly.

Also, scripting languages like Tcl let you construct lines of code inside strings and then execute the string as part of your program, which is pretty cool.

Jeff, I agree with the sentiments about learning some ML - it's not that bad really, and you can do some really elegant things with it. And as has been noted, self-modifying ML code is quite common.
User avatar
Jeff-20
Denial Founder
Posts: 5759
Joined: Wed Dec 31, 1969 6:00 pm

Re: Reflection in a BASIC program

Post by Jeff-20 »

CrAlt wrote:For the love of God, Careful with that! I saw a movie once about a self modifying computer program and it didnt turn out well.
:lol:

@Michael

What I mean is my programs store the user graphics with the code; the graphics are loaded along with the program as if they were lines of code (usually maxing out the memory). I suspect this is a common trick. The first line of the program alters (back to normal) the top of memory and variable pointers once the program is RUN. So I have the graphics, but the program is back to a manageable size.

Of course it could be edited, but that first line would have to be adjusted or the memory pointers would be incorrect.

I am experimenting with what advantages I could find in BASIC with reflection. It's just an experiment. I resist the certainty that nothing can be gained. The very purpose of the experiment is the unusual juxtaposition of BASIC and Reflection. I'm sure ML would be easier and more efficient.

@schema

I fear at one Commodore gathering you guys will have an intervention. I will be forced to admit my reliance on BASIC and enroll in some sort of ML conversion group.
High Scores, Links, and Jeff's Basic Games page.
Post Reply