TS-2068 Programming In The Abstract

See all articles from Update April 1990
Author(s): Bill Jones
Publication: Update Magazine
Pub Details: Update April 1990
Date: April 1990
Pages: 40

THE STACK: The manipulation of the STACK comes under the heading of “where only fools dare tread”. But aren’t we brave? Look at it this way, “you cannot hurt anything by trying, so why not?”. AND, we might be enlightened. Key in the following little program and SAVE it. Then we’ll traipsie through the stack a bit.

10 FOR N=25088 TO 24833 STEP -1
30 PRINT n;"="; PEEK n;: NEXT n
60 PRINT "RETURN Line="; PEEK 25083+256*PEEK 25084'"Statement #="; PEEK 25085
70 STOP: GO SUB 90
90 STOP

OK. First off, we will look at what we can and cannot do with the Stack in BASIC. Timex intended the stack to be “hidden from the user”. But a small tydbyte of information is given in the user manual, page 255. Take a minute to look at the sketch in figure C-1. The little program given above PEEKS this area. The Stack is the TS-2068’s exclusive “library” of “pending things to do”. If <GO SUB> is used in a program line, the LINE NUMBER is factored into a 16 bit word and that word is stored on the stack at address 25083 and 25084. Then the “next statement number” in the line after the GO SUB is stored in address 25085.

In our little program, the GO SUB is in line #70, statement 2. So, if allowed to execute, the number 70 would be stored in address 25083. Zero would be stored in 25084, and 3 would be put in adr 25085. Check this out by RUNNING the program. As the program runs the contents of the Stack addresses will be printed to screen. Somewhere at around address 25000, a train of zeroes will print. These Zeroes tell us the “FREE MEMORY” spaces of the stack. As more and more “library” items are placed on the stack there will be few Zeroes in the train. These “TS-2068 Library items” are put on the stack to be used, and once used they are erased. All of this is done by the internal management utilities in the TS-2068 ROM. BUT, we can have a little influence on things.

When the program runs the first time, a report will be given. This “RETURN LINE # and Statement Nbr.” will be inaccurate. This is because there is no RETURN placed on the stack at address 25083, 25084, and 25085. So, ignore the report on the first run. The program will STOP at line #70. When it does, type CONT ENTER. Then another STOP will occur at line 90. This time DONT do anything for now. Now lets look at what we did.

We have introduced a “piece of JUNK” on the stack, because we did not use <RETURN> after the <GO SUB>. But in this case we do it intentionally in order to see the RETURN in the Stack. After the STOP at line #90, type GO TO 10 ENTER. Write down the numbers given for address 25083, 25084, and 25085, during the first screen full of numbers. Then let the numbers scroll to the end. At the end report the line #70 will be reported, with statement number 3. So, we have captured the <GO SUB that is on the stack>. AND the STOP again occurs at line #70.

Now lets work a bit with 16 bit numbers. First add LINE # 2000 to the program. add: 2000 STOP. What is the 16 bit number for 2000? ans: 2000/256=7.8125. Drop the decimal and 7 is the msb (most significant byte). The lsb (less significant byte) is gotten by: 2000-7*256=208. So, the 16 bit number of 2000 is “208,7”. Now we will CHANGE the line number for the RETURN that is on the stack. POKE 25083,208:POKE 25084,7:POKE 25085,0. After these POKES, Type <RETURN ENTER>. Note the STOP at line #2000. Now we have learned how to change a <RETURN> on the stack to a different line number in a basic program, but WHY would we ever want to do that?

Well I’ll give you a small example and you will think of others. Suppose that after a menu we want to present on screen the data contained in A$. But if A$ has no data, we want the program to RETURN to a different program line than contains the GO SUB. I’ll construct the routine:

500 (MENU item 4 selected)
510 IF Z=4 THEN GO SUB 900
900 IF LEN A$<1 THEN POKE 25083,208: POKE 25084,7: RETURN
910 PRINT A$: RETURN
2000 REM Routine to use if A$ has no data.

RULES: You must have a GO SUB on the stack before POKING the addresses 25083, 25084, 25085. Otherwise you CRASH. So, the POKE must be done AFTER the GO SUB. Dont poke other Stack Addresses, or you’ll get a crash. Oh well, what the heck, and why not?

JUNKY STACK: If you want to find out how fast the stack can be completely corrupted, Remove the Stop at line 70 and change line #90 to read: 90 GO TO 10. Then RUN. One of the pit-falls of using <GO SUB> is that BASIC programs can be broken into. Each time a GO SUB routine is broken into three addresses in the Stack are corrupted.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.