Access Key Details

||  


Book index page

Jump to: Compressing data ||   Using DEF ||   If then Else ||   Sorting data ||    Using Joysticks

Right justification of strings ||   Using VDP ram with Mini Memory Module


CHAPTER SIX : ADVANCED PROGRAMMING

In this section we will consider briefly how the computer works, with a view to making better use of its facilities.

The TI99/4A contains a 16 bit microprocessor, the 9900. This was one of the first 16 bit processors to be made. However, apart from a very small section of memory, the processor communicates with the rest of the console using an 8 bit data line. As a user you do not therefore see the high speeds theoretically possible with a 16 bit micro.
However, the 16 bits are used to make speech synthesis possible, and can also be used by an experienced programmer to speed up some machine code programs.

Most computers place user programs in RAM (random access memory) which is addressed (eg spoken to) by the CPU (central processing unit). This RAM is therefore CPU RAM: it is used by the CPU.
The 99/4A however only has a tiny amount of CPU RAM (16 bit addressed) and when only the console is used, the users program does not reside in CPU RAM.
To provide sprite action, TI have provided a second processor, the VDP (visual display processor) and this has its own RAM, referred to as VDP RAM. This 16k of memory cannot be directly addressed by the CPU.

The VDP RAM is used for screen display, variables, and your program....which the main processor cannot directly address.
Your program is passed to the CPU two bytes at a time, which does not form a particularly fast means of communication, and may be responsible for some of the slow speed of the 4A.
Because there is no RAM addressable by the CPU, you cannot enter or run programs in machine code, unless you add CPU ram in the form of either the 32k expansion card or the 4k mini memory module.
The CALL PEEK and CALL LOAD of Extended Basic operate only on CPU ram, and if you only have the console, you will not be able to find your program in memory.
Only the mini memory module allows you to PEEK and POKE the VDP RAM, using CALL PEEKV and CALL POKEV.

Internal program storage

If you have the mini memory or the 32k ram and extended basic, you may look at how the computer stores your programs.
With Extended Basic and the 32k ram, you may 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).

With this information, you may not only look at your program and see how the program is stored, you may alter the program lines. Using CALL LOAD (or CALL POKEV) it is possible for one program to write over itself and create a completely new program.
 
SAMPLES:
Type in:
100 REM TEX
110 A=B+2
120 C$=D$&"E"
             

Now if you have extended basic and the 32k ram, add:
(works for TI Emulate if you select Extended Basic)
200 FOR I=-25 TO -64 STEP -1
210 CALL PEEK(I,A)
220 PRINT I;A;CHR$(A)
230 NEXT I   
For mini memory owners:
(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 Basic
 MEM: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.
NOTE: Although you key in REM, only one byte has been used.
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.
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.
Note that '2' similarly occupies 3 bytes but that 'B' only uses one byte.
If program memory is scarce, replacing often used numbers with single letter variables can save a little memory. You will appreciate that the number '12345' will occupy 7 bytes! but a variable set to this value only occupies 1 bytes!
(NB: Each numeric variable used also occupies stack space, and will always use 8 bytes of stack space regardless of the number).
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).


You may use this procedure to thoroughly investigate the way your computer stores its programs.

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.

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).
Return to top of page

Accessing VDP ram directly

Using the MINI MEMORY, you have access to the VDP ram, and you can use this facility to change the definition and colour of the cursor, or even to have a sprite or two running in TI BASIC...
Cursor Definition:
The cursor definition is held in VDP RAM 1008 to 1015 (eg 8 bytes). You are used to defining characters with sixteen hexadecimal characters, but your code is translated by the console to 8 bytes:
Each row of pixels can form a binary number of 8 bits, with the left most pixel having a value of 128. Thus a solid block of pixels in one row can be thought of as the binary number 11111111, which in decimal form is:
128+64+32+16+8+4+2+1 =255

For a block cursor, try:

100 CALL POKEV(1008,255,129, 129,129,129,129,129,255) 110 INPUT A$

The cursor will retain its new definition until the console is reset by using NEW or loading a new program.

Cursor colour is defined in VDP RAM 783.
The foreground colour and background colour are contained in a single byte. To separate them you need to divide the byte into two nybbles...

First form the two required colours into binary numbers (binary 0 has a colour value of 1):
WHITE=16 =binary 15 = 1111
TRANSPARENT=1 = binary 0 = 0000
Thus a white cursor on a transparent background would be:
11110000 in binary, which is decimal 240.
To see if this works, try: CALL POKEV(783,240).

Although TI Basic does not recognise sprites, a small part of the memory used by sprites is free, and by placing values there we can place three sprites on screen (stationary):
The sprite definitions must be placed in VDP RAM 768, and are made up of 4 values. The sprite definition must terminate with 208.

The first value is the pixel row (up to 192)
The 2nd value is the pixel column (up to 255)
The third value is the character code (NB: ASCII CODE +96 )
The fourth value is the colour code (-1)

Thus:
CALL POKEV(768,98,128,161,1,
208)

OR FOR THREE SPRITES:
CALL POKEV(768,98,128,163,1,
20,40,164,1,130,170,165,1,20
8) 


The memory area dealing with sprite velocity is also clear, temporarily, but is used by BASIC for the value stack: in normal operation the computer will remove your velocity data as it moves stack data around.
It IS possible to fool the computer though: the top of the stack can be pushed down out of the way by redefining some of the lower case characters (they are usually derived rather than defined, saving memory).
First use:
100 A$="F111"
110 FOR I=96 TO 120
120 CALL CHAR(I,A$)
130 NEXT I          


Now you can move your sprite(s):
Velocity is to be placed in VDP RAM 1920-, and each sprite requires 4 bytes. The first two bytes are for row and column velocity (max 255) and the other two are for vdp use.

Having placed velocities in the correct VDP RAM, the computer must be instructed to move them! This is done by loading the number of sprites to be moved into CPU RAM -31878.

After the above memory relocation try:
200 CALL CLEAR
210 CALL POKEV(768,98,128,16
1,1,208)
220 CALL POKEV(1920,50,50)
230 CALL LOAD(-31878,1)
240 GOTO 240  

Being able to use one or two sprites can permit some advanced graphic work in your TI Basic programs. Each sprite can be positioned to within one pixel on the screen, and can be moved one pixel at a time. (A standard character is 8x8 pixels).

GRAPHICS MODES:
THE VDP chip permits the use of 4 graphics modes, but only three are available with BASIC programs, and only one if you only have the console.
The standard mode is 32x24 characters. This is all that is available to you if you only have the console.
Some utility programs are available giving pseudo hi resolution graphics, which work by redefining characters, but they tend to be a little slow.

TEXT mode allows 40x24 characters.It requires a machine code program to allow you to use it (various utilities are commercially available).

MULTICOLOUR MODE divides each character into four blocks, and each block can be any colour.
To see multicolour mode,try the following:
(Requires Mini Memory or Extended Basic + 32k ram):
100 CALL INIT
110 CALL LOAD(-31788,204)
120 CALL KEY(0,A,B)
130 IF B<1 THEN 120
140 CALL HCHAR(1,1,45,200)
150 FOR Z=1 TO 57
160 FOR X=1 TO 14
170 PRINT CHR$(Z+30);
180 NEXT X
190 NEXT Z
200 CALL LOAD(-31788,224)
210 CALL KEY(0,A,B)
220 IF B<1 THEN 210
230 END    


Enter RUN, then press any key to start the action. At program end,press another key to return to normal. If necessary switch off to return to normal!

HI RESOLUTION MODE allows pixel plotting, but with nearly 49000 pixel positions (plus colour information) there is no room to operate both this mode and the BASIC operating system. It is only possible in machine code programs - such as PARSEC.

Return to top of page

IF THEN ELSE (Logical operators)

IF...THEN...ELSE
TI BASIC may appear to be slightly limited in its use of IF...THEN compared to some other computers. TI do however allow the ELSE alternative.
The problem arises because TI insist that you use the construction only to transfer to another line. You cannot add commands such as:
IF X=B THEN B=C     

to do this you need Extended Basic.

However, TI BASIC does have 'relational operators' which will often help you out of this problem.

The instruction IF X=1 THEN 100 is acted upon by the computer only if the expression (x=1) is TRUE.
A TRUE expression is treated by the computer as having a value of -1, while a FALSE expression is treated as having a value of 0.

The IF..THEN structure does not require an expression to evaluate to 0 or -1 however, and you may use a variable on its own to perform a line transfer:

IF X<>0 THEN 100 will transfer to line 100 if the variable X has any value.
IF X THEN 100 will have exactly the same effect but use less memory.

It is possible in TI BASIC to build up a set of expressions in an IF...THEN line, which will simulate OR and AND, and if you are careful, you may go well beyond OR and AND.

Each expression to be evaluated MUST appear in brackets.
For example:
IF (A=1)+(B=10) THEN 100     
If both A=1 and B=10, then the sum of the two expressions is -2 (-1 plus -1), and as the result is not zero, the transfer to line 100 will take place.
If only A=1, but B=5, then the sum will be (-1+0) or -1, and the transfer will still take place.
If A=3 and B=5, then the sum is (0+0) or 0, and the transfer to line 100 will not occur.

What we have then is a way of saying
IF A=1 OR B=10 THEN 100
(dont type this line in).    
Using a different mathematical operation, the multiply or *:
IF (A=3)*(B=2) THEN 100
When A=3 and B=2, the calculation is (-1 * -1) or +1.
The result is none zero and the line transfer takes place.
When A=3 and B=1, the calculation is (-1 * 0) or 0
The result is zero so no transfer will occur.
What we now have is a way of saying,in TI Basic:
IF A=3 AND B=2 THEN 100.

Provided you are careful to always know the possible results of the various expressions and mathematical operations, you may build up some very powerful IF...THEN commands, which will save you many lines of tedious programming.

This however is not all you can do with relational expressions. How about trying to program IF A=5 THEN B=6 ELSE B=0 in TI Basic?
B=-6*(A=5) has exactly this result. If (A=5) then the calculation is B=-6*-1, or B=6. If A does not equal five, the calculation is B=-6*0, or B=0.

This is fairly advanced programming, but if you need this sort of power, it is there for you to use. All you need is to keep track of the possible values of the variables you use.

To whet your appetite: IF (X=1)+(Y=1)+(Z=1)=-2 THEN 100
This interesting group of expressions will transfer to line 100 if any two of the three bracketed expressions is true. What this fairly short line says to the computer is:
If any two of X Y & Z are equal to one then...

Instead of the = sign, you can use any of the relational operators, = < and >. The same structures can be used with string variables.

The JOYSTICK program following this chapter makes use of these relational expressions.
Return to top of page

Return to top of page

Using DEF

Here are some more advanced uses of DEF

DEF ACS(C)=1.5708-2*ATN(C/(1+SQR(1-C*C)))
All that does is supply you with an ARCCOS function. The trigonometrical functions which TI do not provide can all be made up in a similar manner.
Now in your program when you want to compute the ARCCOS (the angle IN RADIANS of a right triangle formed by the hypotenuse H and one of the sides X. Angle=ACS(X/H) ) you use this defined function. eg ANGLE=ACS(SIDE/HYP)

If you don't like radians, you can amend the defining line,or add a second, after this one:
DEF DEG=RAD*57.29578
Then when you want to convert a radian result, set the variable RAD to the radian angle and whenever you use DEG the computer will use the degree measure equivalent to the radian angle which RAD is set to.
This represents another use of DEF: No 'argument' is passed. The defined variable DEG will, whenever used, take a value which depends on the current value of the variable RAD.


Return to top of page

Using Joysticks

A routine to use the joystick, which we will develop into a rather complex multi-purpose input routine:

The CALL JOYST format is not ideally written for the TI99/4A. The row and column variables are reversed compared to the graphics commands, and the subprogram seems to assume the screen origin is at bottom left (it is actually at top left).

The return variables are placed in the command as follows:
CALL JOYST(NUMBER,COLRETURN,ROWRETURN)

whereas the graphics commands are in the form:
CALL HCHAR(ROW,COL,CODE)

To move a character to the screen left, we need to decrease the value of the column. If the joystick is moved to the left, the column return variable is indeed negative, while movement to the right gives a positive return.
However, the top of the screen is Row 1, so to move down the screen the row must be increased : move the joystick down and the row return variable is NEGATIVE. This can cause confusion very easily! The sign has to be changed to make this work!

Remember: the alphalock MUST be up for the joystick to work.

If we wish to amend ROW and COL variables using the joystick, it is necessary to use:
100 CALL JOYST(1,COLRET,ROWRET)
110 ROW=ROW-ROWRET/4
120 COL=COL+COLRET/4
                


Notice the different signs used to amend the row and column variables. We have to divide by four because the CALL JOYST will only return 4, 0, or -4.

To use the above in a program leaves one problem: you need to know which is joystick number one! This can be marked, but you can also scan both joysticks:

 100 CALL JOYST(1,CR,RR)
 110 CALL JOYST(2,CR2,RR2)
 120 ROW=ROW-RR/4-RR2/4
 130 COL=COL+CR/4+CR2/4  

This will take a little longer to process and you will have to see the effect in your program before you decide to use it.

When using CALL KEY there is a status return we can check to see if NO key has been pressed. With joysticks there is no status return, only the return variables. A status return can however be created.
Instead of the CALL KEY "IF ST=0 THEN" it is possible to use "IF CR+2*RR=0 THEN"
Why multiply the second return by 2? Check through all the possible returns from the joystick and you will see that a simple addition, subtraction or multiplication will not return a unique answer to equate with "joystick central".

So far we are amending the variables ROW and COL without checking to see if they are valid. To use HCHAR etc they must be from 1 to 24 or 32 respectively. Anything else will produce an error message and halt the program.

It is possible to use lots of lines of coding in TI Basic:
200 IF ROW<1 THEN 210 ELSE 220
210 ROW=1
220 IF ROW>24 THEN 230 ELSE 240
230 ROW=24.....
            and so on.
                


It is easier however to add to the ROW incremental line a value check which will reverse the increment if it places the value outside the limits.
To do this we need to use the relational expressions discussed under "IF THEN" in the previous section.
If a relational expression is TRUE it has a value of -1
If a relational expression is FALSE it has a value of 0

Thus PRINT (2=3) will appear as 0, but
PRINT (2=2) will appear as -1.


Dealing with the ROW first, if the variable ROW starts with a value of 1, and the joystick is pushed up, we must reverse the reduction of ROW.
There are two expressions which must be true : if both ROW=1 and RR=4 then after we have added 1 to ROW we must deduct it, to leave it set to 1:
ROW=ROW-RR/4+(ROW=1)*(RR=4)
Now it is impossible for ROW to become less than 1.

This has been developed further in the sample program which you will find printed separately.

A typical use of the joystick is to move a character around the screen, and this is what the sample program will do. To give greater flexibility, this program checks both joysticks, and also checks the keyboard (keys WERSDZXC).
First the screen is cleared and the row and column variables are set to initial values. Our character is placed on screen and the joysticks and keyboard are scanned.
The next line checks to see if an input has been made: if neither joystick nor the keyboard has been used, the program will go back and look at the joysticks/keyboard again. The plus sign between the relational expressions serves as an 'OR'.
If the status of one keyboard unit is NOT zero, the program continues.
Now the program is divided into two. If the keyboard has been used, the variable ST will have a non-zero value which causes the program to branch to the keyboard section. Otherwise it continues with the joystick section.
The joystick section is a slight development of what has been discussed above. We are checking for both limits to the row variable.
The keyboard section uses similar principles,but the limit checks are a little different: If the ROW variable has a value of 1, it cannot be decreased as (RW<>1) takes a value 0 (false) and no change is made.

In the sample program a character is moved around the screen, but if you wish to leave a line of characters, just delete the line which places a blank (32) in the old position.

That was quite a complex program to develop, so check it over thoroughly. The use of relational expressions can become quite complex, but they can both speed up execution time and save memory usage.

JOYSTICK & KEYBOARD CONTROL PROGRAM
100 CALL CLEAR
110 RW=12
120 CL=16
130 CALL HCHAR(RW,CL,42)
140 CALL KEY(1,K,ST)
150 CALL JOYST(1,CR,RR)
160 CALL JOYST(2,CR2,RR2)
170 IF (CR+2*RR<>0)+(CR2+2*
RR2<>0)+(ST<>0)THEN 180
ELSE 140
180 IF ST THEN 240
190 CALL HCHAR(RW,CL,32)
200 RW=RW-RR/4-RR2/4+(RW=1)* ((RR=4)+(RR2=4))-(RW=24)*((R R<0)+(RR2<0))
210 CL=CL+CR/4+CR2/4+(CL=1)*
((CR<0)+(CR2<2))-(CL=32)*((C
R>0)+(CR2>0))
220 CALL HCHAR(RW,CL,42)
230 GOTO 140
240 CALL HCHAR(RW,CL,32)
250 RW=RW-(RW<>1)*((K=5)+(K
=4)+(K=6))+(RW<>24)*((K=15)
+(K+1=1)+(K=14))
260 CL=CL-(CL<>1)*((K=4)+(K=
2)+(K=15))+(CL<>32)*((K=6)+
(K=3)+(K=14))
270 CALL HCHAR(RW,CL,42)
280 GOTO 140


Return to top of page

More powerpul INPUT in TI Basic

ACCEPT AT ROUTINE IN TI BASIC

In Extended Basic you could use:
50 ACCEPT AT(10,12):A$   

but in TI Basic you need to use the routine below and:
50 R=10
51 VR=12
52 IN$=A$
53 GOSUB 100
54 GOTO 54
See below program for more details of how it works.

REMINDER:
Set R & VR to desired row-column
The input is placed in variable IN$
Use GOSUB to enter this routine when it is required
note: Remove line 160 if you do not wish to be limited to entry of numbers 0 to 9.

100 IN$=""
110 CALL KEY(0,K,S)
120 CALL HCHAR(R,VR,30)
130 CALL HCHAR (R,VR,32)
140 IF S<1 THEN 110
150 IF K=13 THEN 250
160 IF (K<48)+(K>57) THEN 110
170 CALL HCHAR(R,VR,K)
180 VR=VR+1
190 IF VR<33 THEN 230
200 VR=32
210 K=13
220 GOTO 250
230 IN$=IN$& CHR$(K)
240 GOTO 110
250 IF IN$="" THEN 110
260 RETURN
    

Return to top of page


The above program is an ACCEPT AT routine in TI BASIC. A PRINT AT routine can be found in VINCE APPS book of TI99/4A programs.

The usual INPUT command causes the screen to scroll, and in the middle of a game with a complex screen display that can be disruptive!

The required routine will allow you to fill a variable string and place the input onto any desired part of the screen.
The initial screen location is held in the variables R(row) and VR(column).

The required input is to be placed in a string variable IN$. To ensure the variable is 'empty' it is cleared at the start of the routine.
The string is filled by means of a series of CALL KEYs, terminated with the ENTER key (which gives a code of 13).
For user confidence the cursor (character 30) is flashed at the position the input will appear at. This comes immediately after the call key. If required a CALL SOUND could be inserted just before the CALL KEY to provide the usual 'input' tone.

If no key is pressed, the cursor will just flash.

When a key is pressed, a check is made to see if it is the ENTER key, to terminate input (Key code 13). If ENTER has not been pressed, you can check to ensure the key falls within a required range.
In the sample given, only the number keys 0 to 9 are accepted as inputs (ASCI codes 48 to 57). If the choice of keys is not so neatly in sequence, you may use:
IF POS("ESDX",CHR$(S),1)<1 THEN 110
If the key pressed is not E, S, D or X in this example, the key is not accepted and the program returns to the CALL KEY.

If the key IS accepted, the letter is placed on the screen in the appropriate location, and the value of the column is increased by one.

A check is then made to see if the input has gone past the end of the screen : if no check was made, a BAD VALUE would be possible.
In this example, if the column exceeds a value of 33, it is reset to 32 and an automatic ENTER is inserted to terminate the input. You may prefer to substitute GOTO 110 instead of ending the input : this alternative places the cursor back on the last position (eg at screen right).
If the key is accepted, the character the key represents is added to the input string and the program returns to the CALL KEY.
Once ENTER has been pressed, a check is made to see if an input HAS been made (a nul input string could cause your program to crash). If no entry has been made the program returns to the CALL KEY.
If all is well, you are allowed to RETURN to the place in your program you left with a GOSUB. The input now lies in variable string IN$ for you to manipulate as you wish.
As the example program has limited the input to digits, a numeric variable can be set by using N=VAL(IN$) in your program.

Additional programs may be found elsewhere in this book, Try to see how they work - can you improve them!

Return to top of page

Right justification of numbers on screen

A quick routine for right justification (useful when using columns of numbers)-the variable to be printed is in the string variable A$ (use STR$(4) etc to convert numbers to strings) and the right column in variable RC:

 X=RC-LEN(A$)
 FOR C=1 TO X
 A$=" "&A$  
(this adds one space in front of A$)
 NEXT C
 PRINT A$
                 

SIMPLE TIPS:

A=B^2 takes longer to process than A=B*B.

A=20000 uses more program memory than A=2E4


DATA COMPRESSION



When memory is limited, any technique which allows you to fit a quart into a pint pot is useful.

A number of data compression techniques have been evolved, some of them quite complex, but we shall deal with the subject only briefly here.

First remember that the TI99/4A stores:
NUMBERS in the number of digits in the number plus 2 bytes.
STRINGS in the number of characters in the string plus two.
NUMERIC VARIABLES in the number of characters in the variabl name,plus 8 bytes of the stack.
STRING VARIABLES in the number of characters in the variable name including the dollar sign,plus stack memory the size of the string plus one.

To store a screen row, column and character value in three numbers takes 12 bytes, in three numeric variables of one letter each, 3 bytes plus stack space of 24 bytes.
Using numeric variables, the stack space is only occupied once for each variable, so if the numbers are used frequently, use of variables may save memory.

Where a large number of such data is to be stored, memory may be saved by compacting each set into a single number:
say Row 12, Column 23, Character 32
can become a single number 122332.

To break the number up if it is in variable A,we can use:

ROW=VAL(SEG$(STR$(A),1,2)) 
COL=VAL(SEG$(STR$(A),3,2))
CH=VAL(SEG$(STR$(A),5,2))
                   


Instead of using a number, it is also possible to place the data straight into a string, say A$, where A$=STR$(A).

Using a string has an advantage in that it may be up to 255 bytes long, and one string can thus contain a great deal of information.

In an adventure program for instance, 255 bytes can easily contain screen information, pointers to standard text, strength and agility counters and so on.

The only drawback is that the greater the degree of compaction, the slower your program will run.

However if your program is impossible without compaction, it is well worth looking at ways in which data can be placed into the computer using as little memory as possible.

An understanding of how the computer uses its memory is vital for this to be effective.
Return to top of page

Sorting

QUICK SORT

Sorting data is a frequent task for many programs, and it seems reasonable to use the fastest sorting method available.
This sorting routine is very fast.
The variables used are:
A,B,C,D,E, F()
A$(), B$

To use the sort, your program must contain the initialisation lines at the beginning. You should not use the above variables in the main program, but if necessary you can change the variable names in this routine to avoid conflict.

The initialisation is here for 200 items. If you wish to sort a different number of items, set C to the number of items to be sorted (line 130) and DIMension A$ in line 100 to the number of items plus one. Then in line 150 set A$(Number of items plus one) to CHR$(124) (looks like | ) (press FCTN and A keys together).

Your program may enter the routine with GOTO (EXIT with GOTO) or with GOSUB (EXIT with RETURN).

The items to be sorted are to be placed in the array A$(), and when the routine is finished, the items will still be in the array, but in ascending order,depending on the ASCII codes of their letters:
eg AA after A, B after AZZZ and so on. A$(0) is NOT used for the items to be sorted, it is a flag.

This routine will sort up to 1000 items. After that, you will need to DIMension the F array- to F(11) for 2000 items, F(12) for 4000 items and so on. The default if you do not use DIM is (10).
 
 {quicksort}

Initialisation:
100 DIM A$(201)
110 A=1
120 B=1
130 C=200
140 A$(0)=" "
150 A$(201)=CHR$(124)

 
       { program }
 
 
2000 IF C-B<10 THEN 2320
2010 D=B
2020 E=C
2030 B$=A$(B)
2040 IF B$>=A$(E)THEN 2070
2050 E=E-1
2060 GOTO 2040
2070 IF E>D THEN 2100
2080 A$(D)=B$
2090 GOTO 2190
2100 A$(D)=A$(E)
2110 D=D+1
2120 IF A$(D)<B$ THEN 2110
2130 IF E>D THEN 2170
2140 A$(E)=B$
2150 D=E
2160 GOTO 2190
2170 A$(E)=A$(D)
2180 GOTO 2050
2190 IF C-D<D-B THEN 2260
2200 F(A)=C
2210 A=A+1
2220 F(A)=D+1
2230 A=A+1
2240 C=D-1
2250 GOTO 2000
2260 F(A)=D-1
2270 A=A+1
2280 F(A)=B
2290 A=A+1
2300 B=D+1
2310 GOTO 2000
2320 E=B
2330 E=E+1
2340 IF E>C THEN 2430
2350 B$=A$(E)
2360 D=E-1
2370 IF A$(D)<=B$ THEN 2410
2380 A$(D+1)=A$(D)
2390 D=D-1
2400 GOTO 2370
2410 A$(D+1)=B$
2420 GOTO 2330
2430 IF A=1 THEN 2490
2440 A=A-1
2450 B=F(A)
2460 A=A-1
2470 C=F(A)
2480 GOTO 2000
2490 RETURN         


Return to top of page

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-NonCommercial 2.0 UK: England & Wales License. Alternative licence: Design Science License published by Michael Stutz at http://dsl.org/copyleft/dsl.txt

Book index page

Access Key Details
Stephen's Entry Page | TI99/4a |Linux |Search |   History St Thomas Church Heaton Chapel | Entertainment | Music Links

Light Reference |   Educational Reference | Science Fiction |   Travel |   News Links | Anime

br>