Reflection in a BASIC program
Moderator: Moderators
Reflection in a BASIC program
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.
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.
- eslapion
- ultimate expander
- Posts: 5458
- Joined: Fri Jun 23, 2006 7:50 pm
- Location: Canada
- Occupation: 8bit addict
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.
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.
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.
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.
Anders Carlsson
6502dude wrote:Your ignorance is showing againeslapion wrote:Correct me if I am wrong BUT...
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.
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
Paul Lambert
Berlin
Federal Republic of Germany
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!
http://www.atarimagazines.com/compute/i ... _BASIC.php
Hope that helps!
Re: Reflection in a BASIC program
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.Jeff-20 wrote:
The program would rewrite/modify itself while the game is in process. Just for the sport of it.
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.
- Mike
- Herr VC
- Posts: 4841
- Joined: Wed Dec 01, 2004 1:57 pm
- Location: Munich, Germany
- Occupation: electrical engineer
@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:
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:
Michael
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
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:
is a strong claim, which - unfortunately - you won't be able to defend.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. [...]
Michael
- Schema
- factor
- Posts: 1430
- Joined: Tue Mar 23, 2004 7:07 am
- Website: http://www.jammingsignal.com
- Location: Toronto, Ontario
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.
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.
Re: Reflection in a BASIC program
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.
@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.