Page 1 of 4

Can't make anything fit into 1K... or is it me?

Posted: Thu Apr 29, 2021 9:24 pm
by LoadError
I'm trying to port a little dice program for the Commodores to the ZX81, and I've squeezed it as much as I could, but I can't seem to make it fit in 1K.
In the ASCII editor I'm using,it is under 800 characters including line numbers and spaces, but when I compile it with zxtext2p it grows to a 1.7 KB .p file, and when I try to load it in the emulator (sz81 with the "-u" switch, which is supposed to emulate a 1K machine) it fails to load (it takes me straight back to the prompt and LIST reveals it hasn't been loaded).
Am I missing something? Is it normal that less than 800 bytes become twice as much when compiled? It's a 21 line program and I can't think of a way to shrink it any further.
Thanks!

Re: Can't make anything fit into 1K... or is it me?

Posted: Fri Apr 30, 2021 1:47 am
by 1024MAK
You do know that the 1k byte (1024 bytes) has to include the screen display file, the input line, the system variable area, the BASIC program and the variables for the program, don’t you?

One hidden item in a BASIC program line, is any literal numbers that the system treats as expressions, have five hidden bytes to contain the floating point version of the number that the interpreter uses at run time.

Maybe you should show us your code?

Mark

Re: Can't make anything fit into 1K... or is it me?

Posted: Fri Apr 30, 2021 12:36 pm
by LoadError
It's the first time I try to do something on this machine, so I have everything to learn still.
So what is the actual amount of memory available to the BASIC program?
Here is my code in T2P format. Please don't laugh at it... again it's the first time I try to do something on this machine. :lol:
---
PRINT AT 1,11;"\BF\BD\A4\9d\::\A9\A6\A9\AE";AT 5,10;"PREMI \9D O \9E";AT 21,13;"FG SOFT"
@scelta:
IF INKEY$ <>"1" AND INKEY$ <>"2" THEN GOTO @scelta
LET D=VAL(INKEY$)
RAND
FOR C=11 TO 15
PRINT AT C,17;" "
NEXT C
FOR M=1 TO D
LET T=10+7*(M-1)
PRINT AT 11,T;"\..\..\..\..\..";AT 12,T;"\: \ :";AT 13,T;"\: \ :";AT 14,T;"\: \ :";AT 15,T;"\''\''\''\''\''"
LET X=INT(RND*6)+1
IF X=1 THEN PRINT AT 13,T+2;"*"
IF X=2 OR X=3 THEN PRINT AT 12,T+1;"*";AT 14,T+3;"*"
IF X=3 THEN PRINT AT 13,T+2;"*"
IF X=4 OR X=5 THEN PRINT AT 12,T+1;"* *";AT 14,T+1;"* *"
IF X=5 THEN PRINT AT 13,T+2;"*"
IF X=6 THEN PRINT AT 12,T+1;"***";AT 14,T+1;"***"
NEXT M
GOTO @scelta
---
For good measure, this is the regular version, running as expected on an expanded ZX81:
---
LET T=0
PRINT "\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::"
PRINT "\::\::\::\::\::\::\::\::\::\::\::\BF\BD\96\A4\9d\::\A9\A6\A9\AE\::\::\::\::\::\::\::\::\::\::\::"
PRINT "\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::\::"
PRINT AT 5,2;"PREMI \9D PER LANCIARE 1 DADO";AT 6,2;"PREMI \9E PER LANCIARE 2 DADI";AT 7,2;"PER CONCLUDERE PREMI \B8\B5\A6\BF\AE\B4"
PRINT AT 10,0;"\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96";AT 18,0;"\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96\96"
PRINT AT 21,9;"FG SOFT - 2021"
@scelta:
IF INKEY$ <>"1" AND INKEY$ <>"2" THEN GOTO @scelta
LET D=VAL(INKEY$)
RAND
IF T=17 THEN GOSUB @cancelladado2
FOR M=1 TO D
LET T=10+7*(M-1)
GOSUB @disegnadado
GOSUB @lanciadado
NEXT M
GOTO @scelta
@cancelladado2:
FOR C=12 TO 16
PRINT AT C,T;" "
NEXT C
RETURN
@disegnadado:
PRINT AT 12,T;"\..\..\..\..\.."
FOR C=13 TO 15
PRINT AT C,T;"\: \ :"
NEXT C
PRINT AT 16,T;"\''\''\''\''\''"
RETURN
@lanciadado:
FOR C=1 TO 6
LET X=INT (RND*6)+1
LET P$="*"
GOSUB @risultatolancio
IF C<6 THEN LET P$=" "
IF C<6 THEN GOSUB @risultatolancio
NEXT C
RETURN
@risultatolancio:
IF X=1 THEN PRINT AT 14,T+2;P$
IF X=2 OR X=3 THEN PRINT AT 13,T+1;P$;AT 15,T+3;P$
IF X=3 THEN PRINT AT 14,T+2
IF X=4 OR X=5 THEN PRINT AT 13,T+1;P$;" ";P$;AT 15,T+1;P$;" ";P$
IF X=5 THEN PRINT AT 14,T+2;P$
IF X=6 THEN PRINT AT 13,T+1;P$;P$;P$;AT 15,T+1;P$;P$;P$
RETURN
---

Re: Can't make anything fit into 1K... or is it me?

Posted: Fri Apr 30, 2021 4:52 pm
by Math123
If you use t2p then you must use switch -d with calling. This minimizes the DFILE. Very importsnt for 1k ZX81s.
Matthias

Re: Can't make anything fit into 1K... or is it me?

Posted: Fri Apr 30, 2021 5:21 pm
by Math123
I tried to run your program on EightyOne with 1k.
Can load it but it uses too much of the screen.
On a 16k setting it seems to run.

If you really want to program for the 1k Memory then you have to modify a lot.

Re: Can't make anything fit into 1K... or is it me?

Posted: Sat May 01, 2021 9:54 pm
by LoadError
Thanks, it looks like I'll have to study a bit...
Not quite clear what the attached code does?

Re: Can't make anything fit into 1K... or is it me?

Posted: Sun May 02, 2021 1:42 am
by bola_dor
As said before.
ZX81 uses most part of this 1K to keep the screen Display File.. luckily if you are running in less than 3K zeddy will use a compressed frame opening it from top left hand. so if you keep it to that corner you will have enough RAM to run it.

Code: Select all

  10 PRINT "PREMI %1 O %2"
  30 IF INKEY$<>"1" AND INKEY$<>"2" THEN GOTO 20
  40 LET D=VAL (INKEY$)
  50 RAND 
  60 FOR C=1 TO 5
  70 PRINT AT C,7;" "
  80 NEXT C
  90 FOR M=1 TO D
 100 LET T=7*(M-1)
 110 PRINT AT 1,T;"\..\..\..\..\..";AT 2,T;"\:    \ :";AT 3,T;"\:    \ :";AT 4,T;"\:    \ :";AT 5,T;"\''\''\''\''\''"
 120 LET X=INT (RND*6)+1
 130 IF X=1 THEN PRINT AT 3,T+2;"*"
 140 IF X=2 OR X=3 THEN PRINT AT 2,T+1;"*";AT 4,T+3;"*"
 150 IF X=3 THEN PRINT AT 3,T+2;"*"
 160 IF X=4 OR X=5 THEN PRINT AT 2,T+1;"* *";AT 4,T+1;"* *"
 170 IF X=5 THEN PRINT AT 3,T+2;"*"
 180 IF X=6 THEN PRINT AT 2,T+1;"***";AT 4,T+1;"***"
 190 NEXT M
 200 GOTO 20
(I am sorry for the chopped parts :D )

ZX81 wastes lots of bytes using floating point variables, so next to do is convert most of the numbers in your listing to non numeric expressions so they are treated as integers.. ie: use NOT PI instead of 0 (cero) or CODE "\.'" (grphics shift T) instead of 6..

as it is now loads and run on 1K but listing is shown in a compressed screen, only top lines are used....
Lines 60 to 80 may be deleted and you will be able to edit from the listing editor itself

Re: Can't make anything fit into 1K... or is it me?

Posted: Sun May 02, 2021 5:52 pm
by LoadError
With these hints, I've managed to accomplish the task.
This is what I came up with:
---
LET Q=1
LET W=Q+Q
LET E=W+Q
LET R=E+Q
LET T=R+Q
LET Y=T+Q
PRINT AT NOT PI,NOT PI;"\::\BF\BD\A4\9d\::\A9\A6\A9\AE\::";AT Q,NOT PI;"PREMI \9D O \9E"
@scelta:
IF INKEY$ <>"1" AND INKEY$ <>"2" THEN GOTO @scelta
LET D=VAL(INKEY$)
RAND
FOR C=W TO Y
PRINT AT C,Y;" "
NEXT C
FOR C=Q TO D
LET P=(NOT PI)+Y*(C-Q)
PRINT AT W,P;"\..\..\..\..\..";AT E,P;"\: \ :";AT R,P;"\: \ :";AT T,P;"\: \ :";AT Y,P;"\''\''\''\''\''"
LET X=INT(RND*Y)+Q
IF X=Q THEN PRINT AT R,P+W;"O"
IF X=W OR X=E THEN PRINT AT E,P+Q;"O";AT T,P+E;"O"
IF X=E THEN PRINT AT R,P+W;"O"
IF X=R OR X=T THEN PRINT AT E,P+Q;"O O";AT T,P+Q;"O O"
IF X=T THEN PRINT AT R,P+W;"O"
IF X=Y THEN PRINT AT E,P+Q;"OOO";AT T,P+Q;"OOO"
NEXT C
GOTO @scelta
---
Fits in 1K.

Re: Can't make anything fit into 1K... or is it me?

Posted: Sun May 02, 2021 6:21 pm
by XavSnap

Re: Can't make anything fit into 1K... or is it me?

Posted: Sun May 02, 2021 6:23 pm
by bola_dor
LoadError wrote: Sun May 02, 2021 5:52 pm With these hints, I've managed to accomplish the task.
This is what I came up with:
---
LET Q=1
LET W=Q+Q
LET E=W+Q
LET R=E+Q
LET T=R+Q
LET Y=T+Q
PRINT AT NOT PI,NOT PI;"\::\BF\BD\A4\9d\::\A9\A6\A9\AE\::";AT Q,NOT PI;"PREMI \9D O \9E"
@scelta:
IF INKEY$ <>"1" AND INKEY$ <>"2" THEN GOTO @scelta
LET D=VAL(INKEY$)
RAND
FOR C=W TO Y
PRINT AT C,Y;" "
NEXT C
FOR C=Q TO D
LET P=(NOT PI)+Y*(C-Q)
PRINT AT W,P;"\..\..\..\..\..";AT E,P;"\: \ :";AT R,P;"\: \ :";AT T,P;"\: \ :";AT Y,P;"\''\''\''\''\''"
LET X=INT(RND*Y)+Q
IF X=Q THEN PRINT AT R,P+W;"O"
IF X=W OR X=E THEN PRINT AT E,P+Q;"O";AT T,P+E;"O"
IF X=E THEN PRINT AT R,P+W;"O"
IF X=R OR X=T THEN PRINT AT E,P+Q;"O O";AT T,P+Q;"O O"
IF X=T THEN PRINT AT R,P+W;"O"
IF X=Y THEN PRINT AT E,P+Q;"OOO";AT T,P+Q;"OOO"
NEXT C
GOTO @scelta
---
Fits in 1K.
You can still take off the

Code: Select all

FOR C=W TO Y
PRINT AT C,Y;" "
NEXT C
Part..
Those LET will save Bytes IF the variables are used several times.. the extra lines are adding bytes .. I didn't make the account. You can use VAL, CODE .. etc in single lines..
There is another very good emulator by XavSnap in this forum that tells yo how are you consuming your RAM..
http://zx81.vb81.free.fr/