|Even Ian Beardsmore can be bitten by the games bug - and memory's no problem when you use his base 255 numbering system!|
I've been described as 'hard-
bitten' and 'jaded' in my time
(And a whole lot else I
shouldn't wonder.Ed.) ... and
you want to know why? Simply
because games of any kind -
yes, even Jet Set Willy - leave
me cold. So, you can imagine
the psycho-physical trauma my
self-image underwent when a
matter of months ago I got
hooked on a wargame by CCS
called Pacific War. Never
heard of it? Don't worry, no-
one else has either!|
Anyway, the upshot of all this is that I decided to write my own version of this great game.
DEALING WITH THE DATAI'd already worked out how I was going to store the data - using a database as the crux of the program - but it was only when I was explaining to a friend that I would need to use three bytes of storage to store the necessary data that I began to see his eyes glaze over. He was perplexed ... and so was I. It seems that what was obvious to me, namely using the character set as a base 255 numbering system, had not occurred to him - or to anyone else I mentioned it to. How do you think that mega- sized dungeons are produced then?
Most of the techniques I'll explain here are quite simple, but the contraction of storage space is phenomenal. First, think of how numbers are built up in decimal and Hex (using base 10 and base 16 respectively). In decimal, when you reach '9', you add another digit - in Hex, having '0' to 'F', you don't need another digit until you reach 'F'. Well, using the character set, you wouldn't need to add more digits until you reached '255', and even then each character only takes up one byte.
Maybe I ought to explain that a little. Each of the Spectrum's keywords, punctuation, etc, are stored as single-byte tokens. So commands like SAVE, LOAD, etc, take up the same storage space as punctuation, such as ',', '!', etc. Thus, if you could store direct commands as strings and then add them together, you could store numbers up to 16,777,215 in three bytes.
Take a look at the first two programs I've included so that you can recap your knowledge of string handling. The first program allows you to input anything from the Spectrum keyboard and output it as a string. The second program takes two strings and converts them into numerical data, adds
|Y O U R||
them together and outputs
them as a string.|
BACK TO BASEOK, now take a look at the third program I've included. This is a decimal to base 255 converter - all you do is input the number you want converted and it prints out the Speccy commands, etc, you'll need to use in your string - and it'll all be stored in just three bytes. Clever, eh?
The program works by finding the larger numbers first. So, variable a and Z$(3) will hold the units of the number, b and Z$(2) will hold the '255's, and c and Z$(1) will hold the '65536's. The example program was written for my own specific purposes. I had four aircraft carriers, each with 100 bytes of data. As I wanted to allocate 175,000 gallons of fuel to the second carrier, the data is stored in three bytes in the field 60 TO 62 - thus, A$ represents the carriers and Z$ the temporary store for the data. In the example printout, you can see that the commands I need to put in a string are '?', ATTR and 'I'.
Number bases are a fairly complex idea, and I am not a mathematician, but the example here does work and can be amended to suit your own particular requirements.
BUT I DIGRESS ...Partly as a digression and also to get your grey matter on the boil, try changing line 95 to read 'STOP' in the decimal to base convertor, and enter the number 17000. No problem - apart from the fact that there's nothing on the screen. Ah, but being a clever YS reader you've probably guessed that CODE 16 is unprintable - try changing the program back the way it was and you'll get your error message.
What I've stumbled on to here is 'protection' - where the PAPER and INK colours are the same ... so nothing much shows up on-screen. Put line 95 back to 'STOP' and enter the number 1052688. The CHR$ would appear to be just visible ... the only problem is that we're seeing the CODE. Now try entering the numbers 1052689 and 1052690; the last number should really put the cat amongst the pigeons!
Next month, I'm going to be dealing with a number of small problems that some YS readers seem to be labouring with. But, in the meantime, if there's anything bugging you (literally!) then write to me, Ian Beardsmore, c/o Your Spectrum, 14 Rathbone Place, London W1P 1DE.
|This program allows you to input anything from the Spectrum keyboard and output it as a string.|
|This routine takes two character strings, converts them to numeric data, adds them together and outputs the result as a character string.|
175000 ? 2 ATTR 171 i 152 ?ATTR i
|A decimal to base 255 converter; simply input the number you want converted and it prints out the relevant Spectrum characters you need in your character string. An easy way to store any number up to 16,777,215 in just three bytes!|
When a REPEAT command is
executed, the value is removed from the
stack. When a REPEAT command is
processed, the line and statement number are placed on the stack (something
similar to a procedure call). When
UNTIL is executed, if the expression is
true, then a value is removed from the
stack and program execution continues
as normal. But if the expression is false,
the top value from the stack is copied and
a jump is made to the correct statement.|
REPEAT ... UNTIL loops and procedures can be nested together. There is more than enough room on the stack for 10 values, but if you try to place more than 10 values on the stack you'll get a 'PROC stack overflow' error message. Likewise removing a value from the stack when it's already empty produces a 'PROC stack underflow' error message.
There are three commands to alter the stack without causing a program jump: POP, PUSH and PCLEAR. POP is used to remove a value from the stack, PUSH specifies the statement and line numbers to be placed on the stack, and PCLEAR clears the stack; the command PCLEAR should always be used at the beginning of any programs involving the use of procedures or REPEAT ... UNTIL loops in order to prime the stack.
GO WITH THE FLOWThere are two commands that can affect program flow - these are MTASK and BRANCH. MTASK provides the Spectrum with a simple form of multi-tasking; it splits a program into two parts, executing a line from the first part and then a line from the second. This 'multi-tasking' continues until either one part of the program finishes or it's turned off. MTASK is always followed by a number which defines the start line of the second part of the program (the first part of the program is assumed to continue after the MTASK command). If you want to turn it all off, execute MTASK 0. Both sections of the program must use the same variables, but each can access different windows and character sets.
The second command, BRANCH, causes a jump to be made to a subroutine after a line has been finished. Bear in mind though that MTASK has priority over BRANCH, so if you want to multitask, don't try to branch as well. The number after the BRANCH command specifies the line number at which the subroutine starts; the subroutine is terminated by an ENDPROC statement. You'll find branching especially useful when debugging a program; take, for example, when a programmer wishes to monitor the value of a particular variable during the execution of a program, this is when a BRANCH subroutine can be used to display its value.
From looking around at other Basics for
home computers - sort of sounding out
the opposition, if you like - one aspect I
was particularly keen to incorporate in
YS MegaBasic was a means of helping
users to structure their own programs.
The 'experts' are constantly suggesting
that you should structure your Basic for
efficiency, but ZX Basic is hardly conducive to that!|
However, I'm more than happy to report that a YS MegaBasic program can be written in structured sections - called 'procedures'. The main advantage is, of course, that each separate procedure can be tested on its own before it's added to the main program. Another useful aspect of working in procedures is that you can name each one; although you may understand each separate subroutine call in one of your own programs, it's not always true that it'll be a piece of cake for someone else who's attempting to debug it later. Using my system of structuring your programs each procedure has a name allocated to it - which should ideally describe its true purpose within the main program. Then, when a particular section is to be executed, all you have to do to bring the procedure into play is to use the name of the section in a program line - as opposed to using a GO SUB statement which doesn't give you any clues as to what's going on!
UP THE ORGANISATIONIn YS Megabasic, the start of a procedure is defined by a '@' symbol along with its specific title - then, if required, this is followed by an underline character and a list of parameters. Note that all the procedure names are converted to upper case letters, and that any existing Basic commands can't be used to specify the procedures, so don't call a procedure 'PRINT' ... use a bit of imagination!
A number of other Basics which include the use of procedures also allow the use of 'local' variables. With that system, variables used within a procedure don't affect the value of the variable of the same name outside that procedure. I'm sorry to say this is not available with YS MegaBasic, so take care when choosing the names of each procedure's variables.
A procedure's end is defined by the command ENDPROC; when a program encounters the ENDPROC command, processing is continued from the statement after the procedure call. Note too that the procedure name can be tagged on to the end of the ENDPROC statement to aid the program's legibility, so 'ENDPROC_DISPLAY' would define the end of the procedure 'DISPLAY'.
I've already mentioned in passing that parameters can be passed to
This system of 'parameter passing' allows
values to be assigned to variables at the
beginning of a procedure for use in the
rest of the procedure.|
STACKING THE ODDSYet another feature of YS MegaBasic is the inclusion of the REPEAT ... UNTIL structure. The REPEAT command marks the beginning of a loop and UNTIL signifies its end. The UNTIL command is always followed by an expression - if this expression is false then a jump is made to the statement after the last REPEAT command and if it's true, then program execution continues from the statement after the UNTIL command.
Both procedures and the REPEAT ... UNTIL commands use a stack to store line and statement numbers. So that when a procedure is called, the line and statement number of the command after the call is placed on the stack - then, when an ENDPROC command is executed, the value is removed from the stack.
|This example program can be used to print a string on-screen in the centre of a line using the procedure 'DISPLAY_A$'. For example, 'DISPLAY_"YS MegaBasic"' is equivalent to the ZX Basic 'LET A$="YS MegaBasic": GO SUB 9010'. Which one would you prefer to use?|
|This YS MegaBasic listing causes the border to flash until the 'Z' key is pressed.|
|Now look at the bog-standard ZX Basic equivalent - frightening isn't it!|
|This program causes 'Task 1' to be printed in window 2 using the standard Spectrum character set, and 'Task 2' to be printed in window 0 using the alternate character set.|