How to save wear and tear on your digits.
By PETER WALKER
FOR all those machine code junkies out there, here is a shot
of code for you to get on with. But for the rest of us I will
look at some of the simpler aspects of machine code on the BBC
Micro.
You may not realise that your computer, even when it appears
to be doing nothing, is actually churning through a very complex
set of instructions from the moment you switch it on to the time
you switch it off.
It is performing a program called the operating system. This
looks after all the operations you take for granted such as seeing
if a key has been pressed, writing characters to the screen, and
updating the clock.
In fact, it is this program that actually runs the micro. Without
it the machine would just sit there doing nothing, not even showing
a picture on the screen.
Within the operating system are many other smaller programs
designed to perform specific functions. Some of the more useful
ones are given names:
OSNEWL - which is used when you press the Return key.
OSWRCH - which handles all the printing to your TV screen.
OSRDCH - which checks the keyboard for key presses.
OSASCI - which puts a character on the screen, and handles linefeeds.
If you are good at puzzles you may see a connection between
the name of the routine and its job. The letters OS always stand
for Operating System, but some of the others seen a bit cryptic.
NEWL is short for NEWLine.
ASCI is simply short for Ascii, which is the character coding
system inside the BBC. It stands for American Standard Code for
Information Interchange.
Now we get to the hard ones. RDCH and WRCH are made of two parts,
RD + CH and WR + CH. These stand for REad a CHaracter and WRite
a CHaracter - all easy once you know.
OSRDCH and OSWRCH are the two I want to deal with now.
The first scans the keyboard to see if you have pressed a key.
If you have, the computer works out which one and generates the
Ascii equivalent for it.
This is then placed in one of the computer's memory slots called
the accumulator. This routine also signals your pressing the Escape
key.
All of the routines the BBC Micro uses can be called for use
in your own routines, by intercepting them and adding your own
"patch" program.
To do this you must change the address to which the computer
jumps when executing OSRDCH. Such an address is called a vector.
Suppose you want to get the TAB key to move the cursor eight
characters
to the right every time it is pressed.
The first thing that must be done is to save all the computer's
internal registers so they cannot be corrupted.
If this is not done the computer might not be able to continue
from where it was interrupted, as these hold some important numbers.
To save them you PUSH these numbers onto the stack by doing this:
PHA - puts the accumulator onto the stack.
TYA:PHA - puts the Y register onto the stack.
TXA:PHA - puts the X register onto the stack.
These variables are then safe from being corrupted by your program.
Also when you have finished your routine these variables must
be reloaded back into the machine as follows:
PLA:TAX
PLA:TAY
PLA
Program I shows how it works.
10 FOR I=0T01
20 P%=&900
30 [
40 0PT I
50 LDA&210 :STA&70 :LDA&211 :STA&71
70 LDA#&15 :STA&210 :LDA#9 :STA&211
90RTS
100 .START
110 STA&80 \Save accumulator.
120 TXA :PHA :TYA :PHA \Save X and Y registers
130 LDA&80
140 CMP#9 :BNE RT \Test for TAB kev.
145 \Lines 150 to 190 Output 8 spaces.
150 LDX#8 :LDA#32
170 .SS JSR&FFE3
180 DEX :CPX#0 :BNE SS
200 .RT PLA :TAY :PLA :TAX \Restore X and Y registers
210 LDA&80 :JMP(&70) \Restore accumulator and return to
original OSRDCH
220 JMP(&70) \Return to original OSRDCH
230 ]
240 NEXT I
250 END
Program I
You then have to put this patch program into the operating system.
You do it by changing the vector.
The vector for this routine sits at address &210. The contents
of this will vary with the operating system you have.
You cannot just overwrite this address, as you need to jump
back to it later on in your program. So your own routine must
save it.
The address is made of two hexadecimal bytes which are stored
in reverse order.
For example, 32768 decimal which equals 8000 hexadecimal is
stored as 00(low byte),80(high byte).
If you look at line 50, you will see I have done this in machine
code at address &70 and &71. I have also changed the vector
to the start address of my patch program.
To switch this program on you must type CALL&0900, after
you have run it. Please note you will see nothing happen, you'll
just get your prompt '>' back up.
But if you then press the TAB key you will find that the cursor
will jump eight character positions along the screen.
This trivial example illustrates a powerful technique. We use
this method in Program II to allow us to enter keywords directly
from the keyboard without having to type them -very useful if
your typing is not quite up to scratch.
With a bit of imagination and not a lot of knowledge you will
be surprised what you can do with OSRDCH.
As with OSRDCH, you must be very careful about saving the registers
when using OSWRCH.
When called it takes the Ascii character code in the accumulator
and writes its pictorial representation to the TV screen.
All characters output from Basic, the operating system and anything
else use this routine. So by using it you could change the action
taken by any of the control codes.
For example, CTRL-G rings the bell once. We could make this
ring more than once. Or you could make the computer print a pound
sign instead of a dollar sign. The list of possibilities is endless.
As before, OSWRCH works through an address stored in a vector
at &20E. Program II uses this routine to incorporate a keyboard
sounder into the computer.
Every time a character is printed it makes a very short beep
using the machine code equivalent of the Basic sound command.
When you press a key it is detected by OSRDCH, which places its
code into the accumulator. This is then passed on to OSWRCH which
prints the character on the screen and makes a short beep.
At the end of a lot of the lines in Program II you will see
program comments. These do not have to be typed in - they just
aid you in seeing how the program works.
This will only work on operating systems greater than 0.1, as
it uses *FX138, which inserts characters into the keyboard buffer.
Once the program has been typed in and checked, save it so you
do not lose it if the machine crashes. To start it just type CALL&0900
as before, but this time it will return with a beep to tell you
it is working.
You should still be able to use the machine normally, except
that there will be a small beep each time you press a key.
If this gets on your nerves, don't worry. Just press CTRL @,
and this will turn it off. Pressing it again turns the sound back
on.
But now for the best bit. If you press the TAB key, then one
of the alphabetic keys, a whole word will appear as if by magic
on your screen. You will find that all the alphabetic keys do
this, plus the @ key.
If, instead of pressing an alphabetic character after the TAB
key, you press the TAB key again, it will give you a totally different
set of keywords.
In all 54 keywords can be entered directly from the keyboard
by pressing at most three keys.
Figure I shows the layout of the keywords. As you can see, most
of the well used ones are actually on the letters they start with.
For example:
PRINT = Key 'P'
INPUT = Key 'I'
MODE = Key 'M'
Do not panic if when you press the key the whole keyword is
not printed, because it was necessary to use some of the abbreviated
forms of the keywords to fit all 54 into the available memory
space. The diagram (Figure I) can be used as a reference guide
until you learn where all the keywords are.
Figure
I
The machine code sound command is an option of the OSWORD routine.
As you know from Basic, the SOUND command is a very powerful and
flexible one and takes the format shown below:
SOUND C , V , P , D
SOUND 1 , -15 , 20 , 200
C = Channel Number (1 to 4)
V = Volume (-15 loudest to -1 softest)
P = Pitch (1 to 255)
D = Duration (0 to 255)
In the machine code equivalent you also need this data, which
must be put into memory somewhere. The address at which you put
this data is pointed to by the X and Y registers, X containing
the low byte and Y-the high.
All numbers are in hexadecimal, including negative numbers.
Each number takes up two locations, so the data will take up eight
bytes of memory.
For the example above, the hexadecimal data for this will be:
01 , 00 , Fl , FF , 14 , 00 , C8 , 00
All the two byte numbers are in the form low byte, high byte.
Program III will produce all the notes from 1 to 255 five times
in quick succession.
10 FOR I=0TO 1
20 P%=&900
30 [
31 LDA#5 :STA&70
35 .START
40 LDX#&40 :LDY#&09 :LDA#7 :JSR&FFF1
80 INC&944 :BNE START :DEC&70 :BNE START
100 RTS
110 ]
120 NEXT I
130 !&0940=&FFF50001
140 !&0944=&00000000
150 END
Program III
To call the sound routine, set up the data in memory and set
the X and Y registers to the address, for example:
Address = &0A00
X = &00
Y = &0A
Then place the number seven in the accumulator - this is the
option number for OSWORD - and CALL address &FFF1.
When using any machine code program such as the keyword program,
it always seems pointless to reassemble the program everytime
you use it.
A better way would be for it to be executed when it had been
loaded. This can be done by *RUNning it. To get this to work first
run the program then type:
*SAVE"KEYWORD"900 B00 900
If you are going to run this from tape you enter, *RUN"KEYWORD",
but if from disc you just type, *KEYWORD.
*KEYWORD is a short form of *RUN"KEYWORD", where "*"
means '*RUN'. Please note, this only works with a disc system.