[Coco] VARPTR not what expected.
Allen Huffman
alsplace at pobox.com
Sun Jul 3 19:36:47 EDT 2022
Charlie, here is some VARPTR code (below).
For a normal variable or string variable, VARPTR returns a pointer to the 5 bytes variable entry.
If it’s a normal number or string, 2 bytes before it will be the 2-character name of the variable.
If the high bit of the second byte is set (128), it is a string. If not, it is a number.
If a number, the 5-bytes pointed to by VARPTR will be the floating point representation of the number.
If it’s a string, it’s a block that tells you where the string is (not in my code below).
FOR ARRAYS, VARPTR returns the first entry in the array. You go back 7 spots and you find the name, then 5 bytes that tell about the array.
I’ll revise this more soon, but this should get you started.
Check out “Color BASIC Unravelled” on page 12 where it talks about this.
0 ' VARPTR3.BAS
1 ' For this to work, ALL vars
2 ' MUST be pre-declared here!
10 DIM AB,AB$,AB(9),AB$(9)
20 DIM V,HL
30 CLS
40 PRINT "VARPT NAM"
50 PRINT "----- ---"
60 V=VARPTR(AB):GOSUB 1000
70 V=VARPTR(AB$):GOSUB 1000
80 PRINT
90 PRINT "VARPT HEADR NAM LENGT #DM DIMSZ"
100 PRINT "----- ----- --- ----- --- -----"
110 V=VARPTR(AB(0)):GOSUB 2000
120 V=VARPTR(AB$(0)):GOSUB 2000
130 END
1000 ' VARIABLES
1010 PRINT USING("##### ");V;
1020 ' Name - 2 bytes
1030 PRINT CHR$(PEEK(V-2));CHR$(PEEK(V-1) AND &H7F);
1040 IF PEEK(V-1) AND 128 THEN PRINT "$ "; ELSE PRINT " ";
1050 PRINT
1060 RETURN
2000 ' ARRAYS
2010 PRINT USING("##### ");V;
2020 ' Header 7 bytes before.
2030 PRINT USING("##### ");V-7;
2040 ' Name - 2 bytes.
2050 PRINT CHR$(PEEK(V-7));CHR$(PEEK(V-6) AND &H7F);
2060 ' High bit means string.
2070 IF PEEK(V-6) AND 128 THEN PRINT "$ "; ELSE PRINT " ";
2080 ' Length - 2 bytes.
2090 PRINT USING("##### ");PEEK(V-5)*256+PEEK(V-4);
2100 ' DIM size - 1 byte.
2110 PRINT USING("### ");PEEK(V-3);
2120 ' # DIMs - 2 bytes.
2130 PRINT USING("#####");PEEK(V-2)*256+PEEK(V-1)
2140 RETURN
More information about the Coco
mailing list