Can a program add basic lines of code on the fly?

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
firelord
Manic Miner
Posts: 557
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Can a program add basic lines of code on the fly?

Post by firelord »

Sorry, if the title is not so descriptive.

I was looking at my mega-hit game 2*65534-in-1-games and I had a thought ...

Lets say we have a program in basic that generates some output. For simplicity I would say that we generate and the following text on the screen :

Code: Select all

DATA 243,234,123,1,5,76,43,22
DATA 3,6,12,76,33,44,55,66
Is there a way (I guess with machine code) that I can add these lines at the end if my BASIC source code?
I mean something like that

Code: Select all

10 
20
...
30 
100 DATA 243,234,123,1,5,76,43,22
110 DATA 3,6,12,76,33,44,55,66
So, after running the original program I have in the end of my BASIC source listing the generated lines.

I hope It is clear what I'm asking.

Thanks
zup
Manic Miner
Posts: 211
Joined: Wed Jul 08, 2020 8:42 am

Re: Can a program add basic lines of code on the fly?

Post by zup »

Yes.

I had a program (from a magazine) that could add a REM line of a given length, so it is possible. But I guess you should use MERGE instead.

If you have to generate a few instances of DATA values, you could include the lines pre-filled it with zeroes (i.e.: DATA VAL "000", "000") and then POKE the values inside those lines. It would be less complex.
AndyC
Dynamite Dan
Posts: 1408
Joined: Mon Nov 13, 2017 5:12 am

Re: Can a program add basic lines of code on the fly?

Post by AndyC »

Ultimately, a BASIC program is just a bunch of bytes in memory. The exact format of which is specified in the manual. So if you POKE RAM correctly, you can generate BASIC code.
sn3j
Manic Miner
Posts: 500
Joined: Mon Oct 31, 2022 12:29 am
Location: Germany

Re: Can a program add basic lines of code on the fly?

Post by sn3j »

You could use the second part of MERGE for this, where the program code is already loaded to a memory block and now needs to be merged with the current program. The best entry point is 8CE I think. Call this with HL pointing to a memory block that is ready to be merged.
The block needs to be identical to the lines you want to merge, ending with $80.

So for example you can write a small program consisting just of the lines you want to add:
110 DATA "abc"
120 DATA 1,2,3

and then scribble down the bytes starting from (23635 PROG) to (23627 VARS), including the first byte from VARS, but this byte - which would be the last of your block - should be replaced with $80. Put this byte stream into a string or something and when you need it to merge, set HL to the start of this stream and call $8CE. You could also use this to introduce new variables without assigning to it.

That's just speculating, however, I am not sure if it really works out.
POKE 23614,10: STOP      1..0 hold, SS/m/n colors, b/spc toggle
firelord
Manic Miner
Posts: 557
Joined: Wed Nov 03, 2021 10:57 am
Location: Greece - Thessaloniki

Re: Can a program add basic lines of code on the fly?

Post by firelord »

zup wrote: Sun Aug 20, 2023 7:56 am If you have to generate a few instances of DATA values, you could include the lines pre-filled it with zeroes (i.e.: DATA VAL "000", "000") and then POKE the values inside those lines. It would be less complex.
This seems to be the simplest solution. Is there an easy way to know where eg line 100 starts (address to POKE I mean) ? Is it with trial and error?

(I can't understand if sn3j describes this using machine code).
sn3j
Manic Miner
Posts: 500
Joined: Mon Oct 31, 2022 12:29 am
Location: Germany

Re: Can a program add basic lines of code on the fly?

Post by sn3j »

firelord wrote: Sun Aug 20, 2023 9:03 pm This seems to be the simplest solution. Is there an easy way to know where eg line 100 starts (address to POKE I mean) ? Is it with trial and error?
Answering for @zup:
If line 100 is a DATA statement you can do a RESTORE 100 and read out DATADD. So 256*PEEK 23640+PEEK 23639+5 points to the address with the DATA opcode.
firelord wrote: Sun Aug 20, 2023 9:03 pm (I can't understand if sn3j describes this using machine code).
It's a different approach and requires a small USR to set up HL correctly.
POKE 23614,10: STOP      1..0 hold, SS/m/n colors, b/spc toggle
User avatar
Einar Saukas
Bugaboo
Posts: 3145
Joined: Wed Nov 15, 2017 2:48 pm

Re: Can a program add basic lines of code on the fly?

Post by Einar Saukas »

BUG90 does exactly what you want.

Try these commands:

Code: Select all

REM xx,yy,zz - creates REM with line number xx containing yy copies of character code zz
DATA xx,yyyy,zz - creates DATA with line number xx containing copy of memory address yyyy with size zz
BASIC - exit to BASIC
Post Reply