The PEEKs to addresses 16392 & 16393 are the system variable for VARS. This system variable is the pointer to where the start of the variables area storage is. Because the variables are stored AFTER the BASIC program, the start of the variables area will move as program lines are added, removed or edited (if the edit results in the length changing). Hence the need for the running program to get the value. This also means that only relative addressing can be used if the length of the BASIC program is likely to change. The same applies if the code needs to store data in RAM, then it is better off somewhere else. “Safe” numbers can be stored in a REM line. If the length of the BASIC program will not change, then these restrictions don’t apply.TMD2003 wrote: ↑Thu Jul 08, 2021 12:30 am So I tried the version above, which meant finding out where it was supposed to be stored; I knew the program was 102 bytes, so after DIM O(51), the value I needed was 16575. That's fine, and I tried the routine to populate the right addresses with the alphabet. However, I'd noticed that if I changed that +2 in line 2 to +28, to account for the 26 bytes of the alphabet characters, the new value of A was 16602... when, surely, it should be 16601.
I tried my re-re-assembled program - putting the LD A,(HL) back into it seeing as there was no need to avoid it now - and entered the 127 bytes into the program above, then replaced it with my BASIC listing to test everything was OK...
...and whether I tried RANDOMISE USR (I+26) or (16601)... black screen.
This is doing my head in.
If the first variable used (after a CLEAR or RUN) is a numerical array, then this will be at the start of the variables area, and the start won’t move while the program is running.
For a numerical array, the +2 in the program that I posted as an example, is to skip past the single letter array name (one byte) and the number total of elements (one byte). Each element is two bytes long. Hence why the total number of bytes is divided by 2.
See Appendix 2, section 2(a) (normally pages 106 to 108) in the ZX80 manual.
So, when using a numerical array, the machine code should always start at VARS +2.
Mark