|★ APPLICATIONS ★ DIVERS ★ RESTORE UTILITY|COMPUTING WITH THE AMSTRAD) ★|
|RESTORE efficiency||RESTORE UTILITY|
Improve your programming style with the help of IAN SHARPE's clever utility
ONE thing that ex Spectrum owners often remark on when they upgrade to an Amstrad is the lack of computed GOTOs, GOSUBs and RESTORES.
In Spectrum Basic it is possible to use the expression GOTO (x'10+1000) which allows you to jump to a line depending on the value of x. In Locomotive Basic you have to substitute this with ON x GOTO or in the case of a computed GOSUB, ON x GOSUB. Program I is an example of the latter.
On the Amstrad, however, there is no equivalent to a computed RESTORE statement which is a shame because it can be a very useful facility.
Often a program reads information from DATA statements into an array. This information is then extracted when it is required. In many cases there is never any need to change the data held in the array - it's just being used as a look-up table.
A trivial example of this is shown in Program II which simply returns the month name from the month number.
This program is inefficient in two ways. Firstly, the names of the months are being held in memory twice, once as part of the program in DATA statements and again in the array month$. Secondly, part of the program is devoted completely to reading the information into the array thus wasting time and space.
Another way of achieving the same result would be to use something similar to Program III. But if a lot of data was involved it would be a very slow method.
Program IV provides a much neater solution, setting up two new commands in the form of RSXs.
The first of these has the syntax:
where 1 is a line number and 0 is an offset, both of which can be computed either before or within the statement.
The line number can be the line with the data statement or a line somewhere before it. The offset is the number of items past the data statement at line I from which you want to start reading.
So giving 0 a value of zero will make the next READ take the first item after the corresponding DATA, whereas making o equal to 1 will jump the first item and start reading from the second, even if the second item is on another line.
This means that you can use DATA statements like one- or two-dimensional read-only arrays. Program V shows you how to incorporate the new command in a routine.
As you can see, if m is greater than 6 the offset m-1 will take the data pointer past the end of line 70 and find the next data statement in line 80.
In all respects except two the new command behaves as you would expect it to if you are familiar with the normal RESTORE statement, except that 1 and 0 can be calculated and 0 allows you to jump a specified number of items.
The first exception is that all DATA statements must be at the start of a line and not after a colon.
The second is that when you renumber a program with RENUM the value of 1 will not be adjusted to suit the new line numbers. To get round this you can use the other RSX added by Program IV which can keep track of lines that have been renumbered. The syntax is:
To use it you simply insert REM statements (which must also be at the start of a line) at points in your program where you wish to identify areas of data, then put a unique string after each REM.
The idea is that you can add a line at the start of the program that will search for this unique string and report the line where it occurs.
For instance if you identify the start of an area of data with the following:
1040 REN data1
at the beginning of the program you would use something like:
10 lin%=0:a$='data1": |SEEK,@a$,@lin%
where a$ contains the unique string. The result will be that tin% will contain the number 1040 after line 10 has been executed.
In this way you can avoid having to laboriously search for and alter all the IRESTs every time you renumber the program. Note that you must use an integer variable to get the line number. Also CPC664/6128 owners can use the string directly in the |SEEK command without putting it in a variable first if CPC464 compatibility is not required.
10 lin%=0: |SEEK,"data1",@lin%
The search routine doesn't differentiate between upper and lower case and will ignore preceding spaces in the REM string. However, it isn't totally foolproof because if your string is also a sub-string at the start of another REM before the one you are looking for, an incorrect line will be returned.
For instance, if you are looking for "data1" which is at line 100 and you have put "data1.0" at line 50, then 50 will be returned as the line containing "data1".
Obviously for a short program such as Program II, any gain in efficiency is more than offset by installing the utility in the first place. However, for anything over a few kilobytes in length that uses a lot of look-ups, such as an adventure game, there can be a considerable saving in memory usage and a corresponding improvement in clarity.
Be careful when typing in Program IV, particularly the data lines which contain the underscore and the tick symbols. These can be obtained by pressing shifted 0 or shifted respectively. Also the case of the letters does matter, so you must use upper-case characters where they are shown.
The Basic loader for the machine code may look a little different to the type you usually see in Computing with the Amstrad and it fact it is. It works in base 64 and has 18 bytes per line plus a checksum.
• A utility was used to create the data statements and this will be published next month.
L'alinéa 8 de l'article L122-5 du Code de la propriété intellectuelle explique que « Lorsque l'œuvre a été divulguée, l'auteur ne peut interdire la reproduction d'une œuvre et sa représentation effectuées à des fins de conservation ou destinées à préserver les conditions de sa consultation à des fins de recherche ou détudes privées par des particuliers, dans les locaux de l'établissement et sur des terminaux dédiés par des bibliothèques accessibles au public, par des musées ou par des services d'archives, sous réserve que ceux-ci ne recherchent aucun avantage économique ou commercial ». Pas de problème donc pour nous!
CPCrulez[Content Management System] v8.7-desktop/cache
L'Amstrad CPC est une machine 8 bits à base d'un Z80 à 4MHz. Le premier de la gamme fut le CPC 464 en 1984, équipé d'un lecteur de cassettes intégré il se plaçait en concurrent du Commodore C64 beaucoup plus compliqué à utiliser et plus cher. Ce fut un réel succès et sorti cette même années le CPC 664 équipé d'un lecteur de disquettes trois pouces intégré. Sa vie fut de courte durée puisqu'en 1985 il fut remplacé par le CPC 6128 qui était plus compact, plus soigné et surtout qui avait 128Ko de RAM au lieu de 64Ko.