Difference between revisions of "Basic Programs in Memory"
Stephen Shaw (talk | contribs) (Major new article on how BASIC programs are stored in memory) |
Stephen Shaw (talk | contribs) m (add link to ExBas) |
||
Line 1: | Line 1: | ||
Internal Storage of BASIC Programs | Internal Storage of BASIC Programs | ||
With the [[Mini-Memory]] or the 32k ram and Extended BASIC, you can see how the computer stores your programs. | With the [[Mini-Memory]] or the 32k ram and [[Extended BASIC]], you can see how the computer stores your programs. | ||
With Extended Basic and the 32k ram, look for your program from memory location -25 to -24576. | With Extended Basic and the 32k ram, look for your program from memory location -25 to -24576. |
Latest revision as of 15:55, 3 January 2015
Internal Storage of BASIC Programs
With the Mini-Memory or the 32k ram and Extended BASIC, you can see how the computer stores your programs.
With Extended Basic and the 32k ram, look for your program from memory location -25 to -24576. The first line entered of your program will 'end' at -25, then as each new line is entered (or edited), regardless of the line number, it is placed on top. If a line is edited, the old line is removed, all subsequent lines change position, and the new line goes to the top.
With Mini Memory, provided you do not have a disk controller attached, your program starts at VDP address 16383 and works its way towards 1536 (or the bottom of the stack if earlier). With a disk controller attached, the program ends at the bottom of the stack and is pushed towards 16383, which makes it harder to look at the program (the locations keep changing).
It is possible to look at your program and see how the program is stored, and you can alter the program lines.
Using CALL LOAD (or CALL POKEV with Mini-Memory) it is possible for one program to write over itself and create a completely new program or modified program line.
SAMPLES
Type in:100 REM TEX
110 A=B+2
120 C$=D$&"E"
With Extended BASIC and the 32k ram, add: (works for TI Emulate if you select Extended Basic)200 FOR I=-25 TO -64 STEP -1
205 PRINT MEM VAL CHAR
210 CALL PEEK(I,A)
220 PRINT I;A;CHR$(A)
230 NEXT I
OR for mini memory: (In TI Emulate select Mini Memory then TI Basic)200 FOR I=16383 TO 16343 STEP -1
210 CALL PEEKV(I,A)
220 PRINT I;A;CHR$(A)
230 NEXT I
Do not edit any of these lines! If you make a mistake start again!
These are the results with Extended BasicMEM:VALUE: MEANING:
-25 0 END OF LINE
-26 88 ASCII code for X
-27 69 ASCII code for E
-28 84 ASCII code for T
-29 32 ASCII code for SPACE
-30 154 CONTROL code for REM
-31 6 LENGTH OF LINE
-32 0 END OF LINE
-33 50 ASCII for 2
-34 1 "1 digit follows"
-35 200 "Number follows"
-36 193 CONTROL CODE for +
-37 66 ASCII for B
-38 190 CONTROL CODE for =
-39 65 ASCII for B
-40 8 LENGTH OF LINE
-41 0 END OF LINE
-42 69 ASCII for E
-43 1 "1 letter follows"
-44 199 "String follows"
-45 184 CONTROL CODE FOR & (concatenation)
-46 36 ASCII for $
-47 68 ASCII for D
-48 190 CONTROL CODE for =
-49 36 ASCII for $
-50 67 ASCII for C
-51 10 LENGTH OF LINE
Notes on memory usage
A lot can be learned of the machines operations in the above manner: it is possible to see how the program is stored, and also possible to learn the control codes for the BASIC commands.
REM
Although you key in REM, only one byte has been used.
GOTO
If you key in GOTO only one byte is used, but GO TO (with a space in between) uses 2 bytes because two command words are used.
String Variable
Note that A$ takes up two bytes but that "E" takes up three: one to indicate 'string',one to indicate how many letters, and then one for 'E' itself.
Number and Numeric Variable
Note that '2' occupies 3 bytes but that 'B' only uses one byte.
Saving Memory
If program memory is scarce, replacing often used numbers with single letter variables can save a little memory. The number '12345' will occupy 7 bytes but a variable set to this value only occupies 1 byte. (NB: Each numeric variable used also occupies stack space, and will always use 8 bytes of stack space regardless of the number).
CALL routines
Although not shown here, the CALL routines occupy 1 byte for the word CALL, but for example COLOR takes up 7 bytes, as it is treated as an 'unquoted string' (command code 200). Because command code 200 is used instead of 199, you cannot use CALL A$. (A$ IS A QUOTED STRING).
Line Index Table
The memory locations -52 onwards in this example hold the LINE INDEX. As program lines do not appear in memory in line number order, but rather in the order keyed in, the computer makes use of an index, which IS in line number order. Each line used occupies 4 bytes for the index:
-52 226
-53 255
-54 100
-55 0
Locations -54 and -55 give the line number (100) and locations -52 and -53 give the location of that line,slightly coded. The memory location is 255x256 + 226 (which is 65506)
Although the computer can address 64k, it does so by splitting it into + and - 32k. To convert this large result to a memory location we can use, we subtract 65535: Location= 65506 - 65535 = -31, which is where the line commences.
As each program line takes up a minimum of 7 bytes: Index=4, Line end=1, Line length=1, Single command=1. it makes sense when memory is tight to use as few lines as possible: this is where you can make appropriate use of the facilities of Extended Basic.
Program that rewrites itself
Here is a short program to show how you can force a program to write itself.
EXTENDED BASIC with 32k RAM:
Key in in THIS order!:
100 GOTO 140
110 PRINT "!!!!!!!!!!!!!"
120 END
130 STOP
140 CALL INIT
150 CALL LOAD(-47,156,199,13,84,69,83,84,32,
67,79,77,80,76,69,84,69)
160 GOTO 110
170 END
After you have keyed this in, LIST it, then RUN it and LIST it again. Note that CALL LOAD has been used to enter several values into memory at one go: the first value goes into -47, the second value to -46, the third to -45 and so on. Although you can overwrite a program line in this manner, the new line must be as long (in internal storage) as the old one, unless you wish to rewrite the line index.
(Line 110 above contains 13 exclamation marks).