Maintaining variable values

From Ninerpedia
Jump to navigation Jump to search

Taken from an article by Peter Walker published in TI*MES 28, 1990.

This article is mainly about using an Extended Basic program to RUN another Extended Basic program, and allows the first program to pass variable values to the second program. It may also work across BREAKs in a program (it depends if you do anything else in the BREAK)

We can exploit character definitions which are not reset between programs and in some cases are not reset at a break.

Consider the two programs below. We wish to pass a character string A$ from PROG1 to PROG2.

If A$ is less or equal to eight characters long, each character can be coded by a pair of Hex characters within a character definition string DEF$.

This is because each character can take one of 256 ASCII values and two Hex characters (0 to F) gives 16 x 16 = 256 combinations.

DEF$ is always 16 Hex characters long. The loop 120-200 takes each character and converts it to a pair of Hex values B1 and B2.

Subprogram HEX1 converts each value to its Hex equivalent. Line 220 stores the definition as Character 127.


When PROG1 runs PROG2 the CHARPAT sets DEF$. The loop 120-180 converts each of the eight pairs to a single character.

Subprogram HEX2 converts each Hex character back to its decimal value. Longer strings can be handled by splitting over 2 character definitions.

Numbers can be transferred by converting to a string, but there may be more efficient ways. For example, the 16 Hex characters in DEF$ could be used to code, say, 13 significant numerics and two exponent characters.

This technique could be used to protect vital data against loss due to an unforeseen break. When the program is restarted, you can include an option to recover data from the character definitions.

Peter Walker

PROG1- the initial program:
100 A$="STRING!" 
110 PRINT A$ 
120 FOR A=1 TO LEN(A$) 
130 B$=SEG$ (A$, A, 1) 
140 B=ASC (B$) 
150 B1=INT (B/16) 
160 CALL HEX1(B1,C$) 
170 B2=B-16*B1 
180 CALL HEX1(B2,D$) 
190 DEF$=DEF$&C$&D$ 
200 NEXT A 
210 DEF$=SEG$(DEF$&RPT$ ( "20",8) , 1,16) 
220 CALL CHAR (127, DEF$) 
230 PRINT DEF$ 
240 RUN "DSK1.PROG2" 
1250 SUB HEX1(A,B$) 
1260 H$= "0123456789ABCDEF" 
1270 B$=SEG$ (H$, A+1 ,1) 
1280 SUBEND 
PROG2- the routine to extract the string:
100 CALL CHARPAT(127,DEF$) 
110 PRINT DEF$ 
120 FOR A=0 TO 7 
130 B1$=SEG$ (DEF$,A+A+1,1) 
140 CALL HEX2(B1$,B1) 
150 B2$=SEG$ (DEF$, A+A+2, 1) 
160 CALL HEX2(B2$, B2) 
170 A$=A$&CHR$(16*B1+B2) 
180 NEXT A 
190 PRINT A$
200 STOP 
210 SUB HEX2 (B$,A) 
220 H$= "0123456789ABCDEF" 
230 A=POS(HS, B$, 1)-1 
240 SUBEND