ZX Basic precompiler

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
Bill H
Posts: 163
Joined: Sat Nov 27, 2010 6:05 pm

ZX Basic precompiler

Post by Bill H »

I am working on a precompiler that will take structured basic code and prepare it for zxtext2p.exe. One of the things I am thinking of adding is inline assembler that the precompiler would convert to a REM statement. To use this I want to have a USR statment in the preceding line and I think I can use the system Variable E_PPC and add an offset of the line length to get me the start of the ML code in the REM statement, is this so?

Bill H
Bill H
Posts: 163
Joined: Sat Nov 27, 2010 6:05 pm

Re: ZX Basic precompiler

Post by Bill H »

I'll answer myself - nope :( - have to try something else
sirmorris
Posts: 2811
Joined: Thu May 08, 2008 5:45 pm

Re: ZX Basic precompiler

Post by sirmorris »

Sorry Bill - I don't think I'm clear about your requirements. Could you post a little example??

I _think_ you mean something like this:

PRINT "HERE COMES THE MC"
[
EMCEE:
ld bc, 10
ld hl, 123
ret
]
PRINT USR [whatever value of EMCEE is]

...

which would produce (for example):

10 PRINT "HERE COMES THE MC"
20 REM $7.;W* INKEY$[9]
30 PRINT USR 16846

??

If this is the case then it's a laudable aim but I don't see the advantage of having a single REM line at the start. Apart from it being an interesting academic exercise that is..!
Bill H
Posts: 163
Joined: Sat Nov 27, 2010 6:05 pm

Re: ZX Basic precompiler

Post by Bill H »

I am thinking psuedocode something along these lines:

Code: Select all

PRINT "HELLO WORLD - THE FOLLOWING IS ML"
{
LD HL, 0
PUSH HL
POP AF
LD BC, 0
LD DE, 0
}
PRINT "THAT WAS IT"
and then the precompiler would turn it into

Code: Select all

10 PRINT "HELLO WORLD - THE FOLLOWING IS ML"
20 RAND USR(ADDRESS OF LINE 30)
30 REM ......... 
40 PRINT "THAT WAS IT"
As I was typing the code I realized that zxtext2p would know the address of line 30 when it is converting the text to the .P file. This would not allow you to edit it on a ZX though.

The other way would be:

Code: Select all

0 REM .......
10 PRINT "HELLO WORLD - THE FOLLOWING IS ML"
20 RAND USR(16514)
30 REM ......... 
40 PRINT "THAT WAS IT"
The ML code held in line 0 could use the rom routines to determine where in memory the line that called it (line 20 in this example) and then be able to get to line 30 and execute it. This would let the code be editable on a ZX.
sirmorris
Posts: 2811
Joined: Thu May 08, 2008 5:45 pm

Re: ZX Basic precompiler

Post by sirmorris »

All fair enough, but then you couldn't edit the BASIC without recompiling the machine code; or relocating it on the fly, or making it position-independent.

Like I said, I don't think there are any advantages to doing it the more difficult way. compile everything into line 1 and you're set..!

C
Bill H
Posts: 163
Joined: Sat Nov 27, 2010 6:05 pm

Re: ZX Basic precompiler

Post by Bill H »

What your saying makes sense, but I can see the need to run a few bytes of ML code on the fly or to setup and jump into ROM to do something. Will see
sboisvert
Posts: 40
Joined: Mon Nov 02, 2009 3:43 pm

Re: ZX Basic precompiler

Post by sboisvert »

Could you not 'add on' the assembly in your 0 REM line as you're processing it, then put in the USR call with the appropriate address for that block of code?

Example:
First assembly code encounter: generates 10 bytes of code, insert call USR 16514;
Second assembly code encounter: generates 24 bytes of code, insert call USR 16524;
Third: generates 5 bytes, insert call USR 16548
...
User avatar
Math123
Posts: 44
Joined: Sun May 11, 2008 11:14 pm
Location: Germany Wuppertal
Contact:

Re: ZX Basic precompiler

Post by Math123 »

Hi Bill,
in my Brainf*ck interpreter I used the following to call the machine-code in a subsequent REM statement anywhere in the basic-listing:

Code: Select all

1000 PRINT USR (5+PEEK 16425+256*PEEK 16426)
1001 REM mc-code in here... 
As discussed before the machine-code in there has to be relocatable. I implemented this with an auto-relocating routine (with patch-table) at the beginning of the mc-code.

This solves the call- and jump-in-problem even with text2p. But as far as I know there is no inline assembler in text2p.
Post Reply