HE MST OF YOUR

_ & spe ae

quar ees

et oe Share ay” gains : a wat * R : 4 a i : oS ee a -

es :

SE Onan s fate oe saat RAR

a

: _ : . _ MODEL SeRACIOUR aN we

OBSTACLE COURSE We look at how an ‘intelligent’ robot may be programmed to move freely around a room, avoiding obstacles in its path

PERIPHERAL VISION A wide variety of add-ons is available for every home cou uter. We give you some valuable a

129

sees . 5 _.

promised last week, our series on spreadsheets continues with a look at Abacus for the Sinclair QL

"THE DEFECT EFFECT Deus Ex Machina

weekly glossary of computing terms

is a novel game that combines elements of arcade-style games with an audio soundtrack ——s showbusiness stars

COMPUTER SCIENCE

FIGUREIT OUT We COVE! the facilities LoGo offers for working with numbers

HANDSHAKING TO HEADER A

NAME CALLING We develop a machine code routine for the Commodore 64 and BBC Micro to complete our search and replace program

DESIGN SENSE Structure is important in machine code, particularly with large programs. We consider some general guidelines

SINE WRITING Now that our D/A converter is complete, we begin to develop the software to produce sound signals

REFERENCE CARD We continue to list —_INSIDE extracts from the Z80 programmers’ BACK reference card COVER

Next Week ©

@ Computer-generated poetry? In our LOGO course we develop a poetry program to demonstrate the word- handling powers of the language.

@ We discover how robots can be programmed to perceive objects by comparing what they see with an internal model. :

_ @ In our 6809 machine code

course we begin to develop a debugger program.

1) Why was Stirling Mouse amazed? 2) What are tape streamers ? 3) What is a saw-tooth wave?

Editor Vike Wesley Art Director David Whelan, Technical Edrtor Brian Viorns Production Editor Gaerne Cardwell Art Editor Claudia Zell; Chief Sub Editor Robert Pickering Designer Julian Dor Art Assistant |i Dixon, Staff WriterSiephen Viaione SubEditerSieve Vann. Researcher Viclanic Davis, Consultant Editor Sieve Colwill, Contributors Geot! Bains, Harvey Mellat, Mike Curtis, Steve Colwill, Chis Naylor Tony Hatingion, Jim Lennox, Steve Malone, [ec Bal Software Consultants (iol Soliware City, Group Art Director Very Neville, Managing Director Slephen England: Published by Orbis Publishing Ltd: Editonal Director Brian innes, Project Development Pete: Brookesmith, Executive Editor Viaurice Geller, Production Controller Peter Taylor-Medhurst. Circulation Director David Breed. Marketing Director Vichae! Joyce, Designed and produced by Bunch Partworks Ltd: Editonal Office (4 Rathbone Place London WIP 1DE © APSIF Copenhagen 1984; © Orbis Publishing Ltd 1984: Typeset by Universe: Reproduction by a Morgan Lid; Printed in Great Bnitain a Press Ltd, Leicester

HOW 10 OBTAIN ISSUES AND BINDERS FOR THE HOME COMPUTER ADVANCED COURSE - Iooues Can De ODiained Dy placing an order with your newsagent or direct from Our subscription department || you fave any dificully Oblaining any back issues from your newsagent please wiite to Us Sialing tlie issue(s) fequired and enclosing a cheque for the cover price of the issue(s) AUSTRALIA clease wiite io. Gordon & Gotch (Aus) Lid, (14 Witham Street, PO Box /6/G, Velbourme, Victoria 3001. MALTA, NEW ZEALAND & SOUTH AFRICA - Gack numbers are available al cover price [rom your newsagent. In case 07 difficulty, write to the address given for binders - UK/EIRE Price 80p/IRE|. Subscription. 6 montis £2392 | Year £4/.84 Binder please send 13.95 per binder, or take advantage of our Special Offer in early issues. EUROPE Price 80p Subscripiion 6 months air £3/ 96 Surace £3146 | year ai £75.92 Surface £6292 Binder £5.00 Aina £5.00 MALIA Obdiain binders fom your newsagent or Miller (Malia) Lid, MA Vassalli treet, Valetta, Malia Frice. £3.95 MIDDLEEAST Price 8Up. Subscription 6 months air £43.94 Surface £3140. | year air £87.68 Surlace £62.92 Binder. £5.00. Airmail. £8.31. AMERIGAS/ASIA/AFRICA - Price. US/CANS 1 95/800 Subscription. 6 months air £51.74 Surface £31.46 | year air § 103 48 Surtace £62.92 Binder £5.00. Amal £9.44 SOUIH AFRICA (ice SA R195 Obiain binders from any branch of Central News Agency or Intermag PU Box 9/394 Springield 2137. SINGAPORE Price Sing S450. Obiain Dinders from MPH Distributors 601 Sims Diive, 03-07- 2\, singapore ‘4438. AUSTRALASIA/FAR EAST - rice 80p Subscription 6 months air. £55.38 Surface £3146 | year alr £110. 76 Surface £62.92 Binder £5.00 Airmail £9.84 AUSTRALIA @rice AusS195 Obtain binders irom First Post Ply Lid 23 Chandos Street St Leonards NSW 2065 NEW ZEALAND - Price NZS? 25 Obtain binders from your newsagent or Gordon & Gotch (NZ) Lid, PO Box 1595, Wellington. ADDRESS FOR BINDERS AND BACK ISSUES Orbis Publishing Limited, Urbis House, Bedtordbury, London woo 4Bl. lelephone 01-379 6/11. Cheques/postal orders should be made payable to Orbis PUDIShing Limited Binder prices include postage and Packing and prices are in 0 Back iSSues are Sold al the cover price, and we do not chalge Carriage in ine UK. NOTE Binders and back issues are oDiainable subject {0 availability of stocks. Whilst every allempl is mace 10 _keep the price of the issues and binders constant, tne publishers reserve te right to increase the stated prices al __any time when circumstances dictate. Binders depicted in this publication are tuose produced for the UK and Australian markets only. Binders and Issues (nay be subject to import culty and/or local taxes, which dre not included in the above prices uniess stated. ADDRESS FOR SUBSCRIPTIONS Orbis Publishing Limited, Hurst Farm, Baydon Road, Lambourn Woodlands. Newbury Berks RG16 /1W. lelephone 0488-72606 All cheaues/postal orders should be made payable 10 UrUIS Publishing Limited, Postage and packaging is included in subscription rates, and prices are given ini sterling

COVER PHOTOGRAPHY BY MARCUS WILSON-SMITH

IAN McKINNELL

In this series we have shown how an ‘unintelligent? wheeled vehicle might be made to move under the control of either a human operator or a computer, and we have looked at the ways in which a robot arm can move ‘intelligently’. Now we consider what needs to be done to design a robot that moves in a truly ‘intelligent’ fashion.

First of all, we do not want to control the robot by using a human operator. If the operator must watch the robot and control its every move then in

many applications there would be no point at all.

in using a robot the person might just as well perform the task the robot carries out. This does not, of course, apply in all situations. Robots used in bomb disposal work are human-controlled, because human expertise is still needed to guide them correctly. |

There is also little point in controlling a robot via a fixed sequence of instructions stored in a computer. This would result in little more than an automaton a device that will slavishly follow the built-in sequence regardless of circumstances. Again, there are times when such a device is useful; robot arms are often considered ‘intelligent’, even though they carry out a pre- programmed set of actions.

However, our definition of an ‘intelligent’ robot was one that would bring you an early-morning cup of tea. This cannot be human-controlled, as its function is to carry out its task before a human is awake. If this tea-bringing device is programmed with a fixed sequence of instructions, problems

will arise if you move your bed or leave a pile of

clothes on the floor.

So our definition of intelligent movement is the ability of a robot to move around in its environment without being controlled by a human and without blindly following a fixed sequence of instructions. It should be able to travel from one point to another, avoiding any obstacle on the way.

There is a tradition in the field of artificial intelligence of using games of one kind or another to examine complex problems of this sort. Just as

_chess-playing programs have given considerable

insights into other branches of artificial intelligence, so maze-running robots can help in the definition of truly intelligent movement. In the late 1970s, ‘micromouse’ contests began in the USA, and in 1980 the first such competition was held in Britain. The idea was very simple a large maze some three metres square was constructed, and contestants had to design robot ‘mice’ that could find their way unaided to the centre. The maze consisted of small squares of equal size, the sides of which were sometimes open to show a

Building Sight

lf their processors are sufficiently sophisticated, mobile robots can learn a catalogue of archetypal objects for use with shape-recognition and pattern-matching algorithms. The Beasty robotic arm here is equipped with a Snap camera, which produces a digital picture, and the Snap software, which includes an object recognition module. Once the object has been ‘seen’

from different viewpoints, the arm has a reasonable chance of being able to recognise it in any position

THE HOME COMPUTER ADVANCED COURSE 721

KEVIN JONES

BRIAN MORRIS

APPLICATION :ObOJICS

Simple As ABC

oiple aigonthams do work in maze solving. (his obol advalices 110 enply Spaces | Unt) itineeis a dead end ai ange io

exampie. [linen retreats (0 ine las| (UNCON ILencounlered Square G ere (naiking aliine intervening Squares as useless in tS enial ap. || inere are 0 untned routes 10m a junciion ine O00 1etreals (0 (ne junction Delore (al, and SO On, all tne Way Dack (0 (Ne enirance | necessaly i) which case Tie mazeis Dind OF insoubie

Squares F

Pues PG

a pis Ba eer

oe Fae B

Sy eee ee Ga ate ee UE eae ees ee cc A laa

Solving The Maze

He ae le ee ae HA ae 4g LAR ee ee ee ea te eA

Ce UD er PA

CU ee a

oe 2o0p oo.

a eee ee ay ena z 7 ee EC

|. | . =. a =,,—Cr—.—.UCUCUTU—OC KNVY.—Cs‘a‘cCawsiC*ztC

A9a9 7000 700)

Le ee ee ee ee oe ae Fae Ps Ee & SO Se ee A ee ee eae Le a e

Pee pe ee ee,

Pe eee A ee LD

DL ee A eae ee eee ee TEN ea DESO 1 2g eee

PORE SL eee ee ee Me ee

FE ES a Re a A ae a ea A ee es Aa a a Bee a ea ND * EA ae ee ts eae a ee ce ae ee ee ae aa a ee Reena hse te a Leg ee Pee ee

Ce Rs i ee a)

TP OR ee Te Ne ee Pee CMR ye De ee Pe Be ee PAD ees ee

PM TR Ee

Ee | ea Ey aA CREAN Beige a) Bay) a Boge usta Sey athens He es Ga ee eA A arent Ta ew a ghar a) an See aes ee a a a aan Hah aes pr ae, Beary

eaeean nae

722 THE HOME COMPUTER ADVANCED COURSE

FLEE PPA 3 AC 0 A Se Ae Be AC ea Ae ee i a ae

Ae PMTs x

BT Seg Ui Se Ae RE ae A a ie RR ie Ae i a Se ey oF

Oe 1 eo poe eeae

1) 2 ee ee ee 2) PRU GPL eke) vey cp) Dee er ee eee 1 ee ee

Pelee

Pa PASE Oe EAC A ie i ea i a ie ie A nas i i i

Pee PRt Te mage x

Aas Trea. ae Ge a Ss ae Ue ae Gee cas eet cee eae ee ca ces See

PRIN] 06.6 oe coe! Weal ee Cee Ls

Ca "

POP el 0 Glee eee eee Peet bee ee Gr) ue

Pot ee ee

CR ope ee

Pe ee ae ee

Wie MCRL) Ce Nin Cia}

Pie US IN INL sey ee ed GOB FOS Pit) WEN]

COM C ee eR ) e O P

PR UND M2 (00-1 RO) eae

Oe Gy: COBO: GUSUBOEAG: PRINT)? Re TURN :

BS Ee A ori ts ce Ua a a io i ie ch

ReMe POSITION THE CRSA » BT a Ne A a RB i i ie A ie

PRINT LEP TERS RO) TAR (CO 1) st Re TL

ENTRANUE

possible route, and sometimes closed to denote a wall. The mouse that reached the centre in the shortest time won the contest.

At the first British Micromouse contest, there were five entrants only. Some of these behaved in an extremely erratic fashion one could not even travel in a straight line and even the best of the mice became quite bewildered once it had turned a couple of corners. In the same year, the European Finals of the competition were held, and mice began to arrive from Finland, Switzerland and Germany. Eventually, a mouse did succeed in negotiating the maze correctly; this was Nick Smith’s ‘Stirling Mouse’, which was equipped with simple mechanical sensors that ran along the top of the maze walls and was powered by a simple stepper motor. Since then, interest in such competitions has grown, and in the 1984 Euromouse Contest in Madrid the fastest time to the centre of the maze was 31.4 seconds. Some contestants were still unable to reach the centre at all, but most succeeded.

MAPPING THE MAZE

So how does a robot mouse negotiate a maze? In general, the robot must have a precise method of moving itself around so that it knows its exact position at any time this can be achieved by mounting the robot on wheels and driving it with stepper motors, often using some form of internal position feedback, such as shaft encoders. The robot also requires a set of sensors to detect the presence or absence of walls so that it can

construct a ‘map’ of the maze. In Micromouse

contests, the robots are allowed a couple of training runs, which they use to work out a plan of the course. They then make the competition run, during which they are timed in their attempts to reach the centre. |

Although precise methods vary from one robot to another, one answer is to have the robot fitted with a simple tactile sensor at its front. Sitting at the centre of each square of the maze in turn, it can test to see if a wall is directly in front of it. It then turns clockwise through 90°, tests again, and repeats the sequence. Eventually it will ‘know’ where all the walls are in each square of the maze. This information can be stored as a single four-bit binary number so 1111 in binary would represent a square with walls on all four sides (impossible in practice, as the robot could never enter that particular square), and 0000 would represent a square with no walls at all. 0111 would then represent a square with three walls and one opening a cul de sac.

This information could be held in a two- dimensional array in Basic, DIM A(16,16) could be used to represent a maze with 16 ‘cells’ in each direction. The robot then has to work out a route that will take it to A(8,8), if that is considered to be the centre of the maze. Often the robot has a built- in computer program that works out a tree structure for each route through the maze. Many of the branches of the tree will lead to dead ends or

KEVIN JONES

bring the robot back to a point it has already

visited; in these cases the branches are ‘pruned’ and disregarded. The program then searches along the remaining branches to find the route with the least number of squares. It then adopts that Pa as its route to the centre.

This method can be adapted to provide a more

efficient strategy. The sensors on the robot are.

crucial to its success. For instance, simple mechanical touch sensors require the robot to

actually bump into each wall to map its path;

proximity sensors can detect a wall without actually touching it and a distance sensor can detect the position of a wall at the end of a long clear path in the maze. Obviously, equipping the robot with four sensors instead of just one would enable it to ‘look’ in all four directions at once and

would remove the need to make it turn around in.

each square.

AROUND THE HOUSE

So we can see that a robot can act ‘intelligently’ as it negotiates a maze. In many respects, the problem of constructing a robot that can find its way around your home is very similar. The robot must use sensors to work out the positions of all the objects in a room, and it must then plan a route that will take it round any obstacles to its destination. The additional problems involved in this type of intelligent movement stem from the fact that a room is much more complex in design than a maze. The typical room is not neatly divided into squares, nor do all its contents remain

in the same place. Your tea-making robot may learn the position of various objects but if you move a chair, or if a cat sits on the floor, the robot must then modify its chosen path.

This problem can only be solved by having the robot make continual use of its sensors to update its internal map. The problem of the cat requires more thought because, as robots do not know

anything about cats (or about people, for that

matter), it is difficult for it to work out what to do at its first feline encounter. (No doubt the cat will have the same problem when it first meets a robot.) The best solution is to fit the robot with a

movement sensor which is a distance sensor that

responds to variable distance measurement and can thus cope with moving objects. Once the | moving object has been detected, the best thing the robot can do is to stop moving altogether until the object itself stops moving or goes away. This may not sound very intelligent, and is certainly less friendly than going up to the cat and stroking it, but such an action is very similar to the reaction shown by many animals, which ‘freeze’ when they detect moving objects.

The whole subject of intelligent movement is thus intrinsically linked to the use of sensors in conjunction with a computer program. A robot without sensors will not be able to move intelligently, and the more sensors a robot is equipped with, the better its knowledge of the world will be. It is this knowledge of the world that enables the the robot to exhibit signs of intelligence.

A Room Of Gne’s Own

Finding a way around unknown objects is never easy. Path A shows the track of a simple household robot trying to find the electrical socket. Its only object avoidance algorithm (known as ‘wall-following’) is to follow the edges of things while its short-range sensors seek the » object. This primitive method can be very successful in simple Surroundings, but is susceptible to traps and pitfalls of the kind shown here.

Path B shows the track of a similar robot with a slightly improved algorithm: when it has to turn along the side of an object it prefers to turn through the smallest possible angle since this will reduce the amount of ‘back-tracking’ that it does. This simple change greatly reduces its vulnerability to traps and allows its scanning sensors to control its behaviour

THA more effectively

THE HOME COMPUTER ADVANCED COURSE 723

MODEL BEHAVIOUR

Our series of articles on | spreadsheet

modelling has so far concentrated on Psion’s Vu-Calc, a simple cassette-based package for the Spectrum and BBC Micro. Here we tum our attention to Abacus the spreadsheet package, also designed by ts au eee with the Sinclair bid

has centred on the four software goede

‘supplied with the machine: Quill (a word processor), Archive (a database), Easel (a

graphics program) and Abacus (a spreadsheet). These packages contain some elements of integrated design (see page 502). Data may be transferred between them; spreadsheet models, for example, may be displayed as graphs or incorporated into a document prepared using Quill. The display screens are similar, and some commands are common to all the packages: for example, three of the QU’ five function keys give identical results in all four applications (F1 is the Help key, F2 controls the ‘prompts’ area at the top of the screen and F3 calls up commands). However, the programs must be loaded and run separately.

There are two different ways of loading Abacus (or indeed any of the QL packages). The first involves putting the Abacus cartridge in Microdrive 1 and then pressing F1 to select the monitor option or F2 if a television is used as a display. The OL packages include ‘boot’ routines, and the program will thus load automatically. Alternatively, if the screen is already selected, enter lrun mdv1_boot (assuming Abacus is in drive 1), and the initial screen will appear.

The screen shows the top left-hand portion of the spreadsheet matrix Psion refers to this as a ‘srid’ in its documentation. Initially, columns A to F and rows 1 to 15 are displayed, although Abacus has a maximum grid size of 64 columns and 255 rows. (Compare this to the maximum Vu-Calc grid size of 28 columns and 55 rows.) Above the grid display is a collection of prompts, and below it is a data entry line, together with information showing the status of the current model. The prompts can be removed (by pressing F2), but are very useful for beginners as they explain the choices available at any time. This is, in effect, a ‘menu’ area, indicating which function keys are used to control various operations, and showing how to move the cursor or go directly to a particular celi, how to enter data or text, and how to call up commands. It is not a true menu, however options cannot be selected by

724 THE HOME COMPUTER ADVANCED COURSE

positioning the cursor over the relevant choice, but must be typed in by the user. Using Abacus for basic tasks is very simple,

although for more advanced modelling some

commands and expressions will take some time to get used to. The following example, again based on a home budget, will illustrate Abacus at work. First of all, we need a general heading. As with Vu-Calc, text entries must be preceded by a double quotation mark. We will call our model CASH FORECAST and, by pressing the appropriate cursor key, we move to cell D1 and simply type a double quote followed by the text. Abacus, like most spreadsheets, allows text to ‘overflow’ a cell if the adjacent cell is empty, so it is easy to enter even long titles anywhere on the grid.

CASH FORECAST

We can also underline the heading, thus improving the appearance of our model. To do this, we must move the cursor to the cell below our title (D2) and enter rept (“=”,len(d1)). Here, rept is the equivalent of Vu-Calc’s REPLICATE command, and = tells the program which symbol to use (we are using the ‘equals’ sign as a double underline). The rest of the command len(d1) is a neat way of telling Abacus to repeat the symbol for the length of the text in cell D1.

Unlike Vu-Calc, which has a fixed column width of nine characters, Abacus allows us to select different widths for different applications. Here we need to make column A wider, to allow enough room to enter text of varying length, and we also require the other columns to be made narrower, so that the date for six months can be displayed. To do this, we use the function key F3, following this with G (to select the GRID command) and W (for the WIDTH command). The input line will indicate that the current width is 10. We want to change the width of column A to 15, so we enter 15 in response to the prompt. The program will now prompt for a range of cells to which the new width will apply. Entering A and A as the two parameters indicates that only column A is to be set to this width. We must then go through the same procedure again, this time selecting a width of 6 and a range of B to G. There is now sufficient room for six months’ figures to be displayed.

We must now enter labels for the ‘months’ columns. These may be done by simply typing the relevant text in each cell, but Abacus has a special facility for calling up month names. Move the cursor to A3 and type row=month(col()-1). The input line will now prompt for a range enter B and G in reply to the prompts, and the columns will be labelled automatically. ‘January’ and

————$———————

#

x,

‘February’ contain too many letters to fit into our adjusted columns, so these must be abbreviated. This is done by overtyping, remembering the quote marks to indicate text.

The next step is to enter headings for the various rows, moving the cursor down a line after each entry (unfortunately, Abacus does not provide this as an automatic facility). Our model assumes that the householder is a salesman whose income is made up of both basic salary and commission. The model allows income to be calculated on the basis of commissions on forecast sales plus basic salary. Actual sales achieved can be entered as the months pass, and revised forecasts can then be made for future months. Any negative values are displayed in brackets on the finished grid. These are the headings to be entered: Sales: forecast and actual; Commission; Basic salary; Total income; Expenses: Mortgage, Rates, Water rates, Electricity, Gas, Telephone; Total expenses; Net in or (out); Opening and Closing bank balance.

After column and row headings have been entered, we can begin to fill our spreadsheet with

figures. First we'll enter the sales forecasts for the six months displayed on the screen. These are £10,000, £12,000, £13,000, £11,000, £12,000 and £15,000, but should be entered without the pound sign or comma. Actual sales can’t be entered yet, so well continue by calculating commission on the forecast sales. This is calculated as 20 per cent of sales over £10,000, so the formula to be entered into cell B10 is row=(sales-10000)*.2. Once again, the range required is B to G; enter these letters in response to the prompts and the commission will be calculated and instantly displayed.

If the basic salary is £1,000 per month, this can be entered at cell B11 as row=1000, again over the range B to G. The formula for total income may now be keyed in at B13. This is row=sum(col) witha row range of 0 to 11 and column range B to G. This completes the income calculation, but the presentation of the model can be improved by adding rules above and below the ‘total income’ row. To do this, move the cursor to B12 and enter row=rept(“—” ,width()-2). Abacus will respond by producing four dashes under each ‘basic salary’ figure. To carry out the same operation on row 14, thus producing dashes above and below the ‘total

income’ row, the ECHO command is used. With the cursor still at B9 the system will prompt for a range over which to reproduce the underlining respond with B14:G14. (If you forgot to leave spare rows when the headings were entered, these can be inserted by using the GRID command.) To complete the formatting, all the underlining should be ‘justified right’ by using the J command over the range B2 to G14 . This will ensure that all the dashes will be aligned to the figures. Expenses may now be entered, using the row formula for standard monthly figures such as the mortgage, or by simply entering each figure into the relevant cells for occasional payments like electricity and gas. Total expenses may then be defined as the sum of all these figures, in the same way that the total income was calculated. Similarly, the net amount in or out can be calculated as the total income minus the total expenses. Unfortunately, Abacus appears to ignore any part of acomman4d that follows a space, SO we cannot simply enter row=total income-total expenses. We must therefore rename the total

eenecrenzezeca

Jonuaryrebruartiarch fiprit fey Bh

income line as ‘income’ and enter row=income-total, again for the range B to G. Each month’s net inflow and outflow will now appear on screen. The final step is to calculate the bank balances. First enter the initial balance an overdraft of £200, perhaps. Enter this as -200. Now calculate

the closing balance, using the formula row=net+opening with the cursor at B28, over the now-familiar B to G range. This will give January’s closing balance. Opening balances for other

months are calculated by entering at C27 the

formula row=B28. To make this work over all columns, it is first necessary to change the order of

calculation from row to column by using the

DESIGN command. All opening and closing balances will then be calculated, and will be recalculated immediately if any figure is changed. As it stands, our model will depict negative balances as figures preceded by a minus sign. To change this format so that brackets signify a negative figure, we use the UNITS command and reply to the prompt with B.

Our model is now complete. It should be saved by selecting the SAVE command and an appropriate file name chosen. The model may then be printed out, or different figures entered.

THE HOME COMPUTER ADVANCED COURSE 725

Spreading The Word

Attractive screen design, good features and a large prompt/ help facility makes Abacus a pleasant and easy spreadsheet in use. Here we show the ECHO command in use copying one row to another, the DESIGN command options for formatting the spreadsheet, and the Month feature producing the month names at the press of a key. Also featured are the default screen with the Prompts menu at the top, and the blank sheet showing the Command menu

IAN McKINNELL

—a

Intelligent Copy

The replace routine has to move large sections of the BASIC program up and down in memory when it inserts new variable names of different lengths. In this it encounters the four possible conditions of the source and destination addresses. If it always copies from the start of the source block then, when the head of the destination block overlaps the tail of the source block, the copying will overwrite some of the source data. An ‘intelligent’

copy routine will detect this case

and avoid corruption by copying this source block from the tail first. A ‘dumb’ copy always copies from the head of the source block

LIZ DIXON

NAME CALLING

Having looked in more detail at the way a BASIC program is stored, we can now extend the variable search program to include a facility to replace one variable name by

another. Here we look at the BBC Micro

and the Commodore 64 versions; in the next instalment we will develop the same program for the Spectrum.

Our variable replace program is a more demanding utility than the simple search for variable names that we developed on pages 664 and 700. For this reason we need to add a

726 THE HOME COMPUTER ADVANCED COURSE

machine code program. The BBC Micro’s 6502 CPU and the Commodore 64’s 6510 CPU have the same Assembly language, so it is a good idea to look at them together.

Our first task is to find a method of holding two separate programs in the computer at the same time. As we have already explained, we can do this on the BBC Micro by altering the built-in BASIC variables PAGE and HIMEM. On the Commodore 64, we need a machine code program to alter the various pointers in zero page memory. The first part of the Assembly language listing, beginning at the label SWITCH, will do this for us.

The routine SWITCH will enable us to accommodate two BASIC programs: one beginning at address 800 hex (the usual place for a BASIC program); and the other beginning at address 9000 hex. SWITCH begins by looking at the pointer TXTTAB to see which of the program areas is current, and then changes the pointer values to make the other program area current.

TXTTAB is changed to point to the start of the new program area, then FRETOP and MEMSIZ must point to the byte after the last byte of the new program area, while FRESPC points to the end of the new program area. The program then searches down the chain of link address pointers (see page 704) to find the end of the Basic program, using VARTAB as the temporary pointer. When it finds the two zero link address bytes that mark the end of the program, it increments the previous pointer twice and copies the result into ARYTAB and STREND. In this way VARTAB, ARYTAB and STREND all point to the byte immediately after the BASIC program.

The main changes to the BAsic program that we need to make are the extra subroutines at lines 30500 and 30600. The first of these finds the end of the BASIC program, using the length of line bytes in the BBC version and the next line pointers in the Commodore 64 version.

The subroutine at line 30600 actually makes the change in the variable names. When the old and new variable names are the same length, the new name can simply be written over the old. Where the old and new variable names have different lengths, the procedure is a little more complicated. In this case, the program must either make extra space, or close up any unneeded space in the program it is changing, and make corresponding changes to the variables it uses to keep track of its position in the program being altered. It must also change the length of line byte in the BBC version and the next line pointers in the Commodore 64 version.

DO 000000

= ||

foo 000000 |

size ed

th thousands of bytes to be mov

e code Although

would be

it

machin

2)

: ag S aS

$3 <¢5 RByAs = | ® 3.8 Cy* Ba a—aO8 gS: 6,7 © Ae. 2 mee >. Boas * S86 0205 8 =i © &b B2e 7) Ses

UP to

es

every time. There are two machine code routin

make extra space; and DOWN to close up unneeded space. Details of the block of program

to be moved are passed to the mac

through the memory locati

e code LAST an

d

2

ons CURR

DIFF.

wes

owing memory

alised

address of bottom of block to be moved

one less than address of top of block

moved

P, the foll

ti

ini

For the subroutine U locations need to be

to be

CURR LAST

S

number of bytes to be freed

and for DOWN they need to be

DIFF

alised as

address of top of block to be moved one less than address of bottom of block

to be moved

ti

ini

CURR LAST

ed

DIFF

he

RUN the Assembly language program to put t

art at opposite d the

it has been moved

avol ble replace

arla

to

1S

S

in (or LOAD) and Then LOAD the

es st ections. data being overwritten before Ory. in

te

number of bytes to be recl

Note that the two subroutin

moves in opposi To use the BBC version of the v.

program, you first need to type

ends of the block to be moved and make the

program to be altered and type

machine code into mem

= PAGE

P%

oad ow Sots Suse O50 Axx & bb 5 ae ba Sha oe Anak S be PE oe 2 Se BSL o & SESE o8— so So Saeae SD Oo Aw O = 42's o's BS oS 2 = 6-0-6 Es eso

= PAGE 1

HIMEM

OoOges 3S vO Segoe 83828 Son 3 aso & = © 2a 205 Dn a 5 EF Bo eo 25 eres mes 0 YOY mos BOE 08 Su Les 3 of gece 8 ROORS 2 865 U'd un SG 2g Fox see6° D5 gha ks bb 5 bb 5 ORZE BASSAS

se!

d

to ing the alternate

you will need , 36865 an

. :

ivalent to NEW

is equ

into addresses 36864

S

directly as machine code POKE zeros 36866.

program area

code

i

fh SS2E5 Pre oe p= OW. © Ve) © T a & See, > mee o SOosss 72 hE SegsL Boda S 25524 e ) Nee Baggs > Bb eS See er Saved oer2s Me 6's . Or ee} B= o = § se xs sfag 5D ESE gba. § BEd 2 Sea. &.

areca

the

in am to be altered d the variable

chine code alternate program lacement program

into the ble rep

you have the ma computer, you can LOAD the progr aria

then RUN the v.

?

Once into the normal program area an

replacement program

area

THE HOME COMPUTER ADVANCED COURSE 727

Hard To Say . arate ‘Winchester’ has become the generic name for all hard disk

drives, though properly it refers.

only to those that employ IBM’s Winchester

_ technology. Computing legend has it that

_ this name- derives from the prototype ‘Machine's having been called a 30/30, which is the Calibre and load designation of the famous Winchester rifle. The more prosaic explanation is that the technology was developed at IBM’s plant in Winchester, England

HANDSHAKING |

In computing, the precise timing of operations is essential. Even such microprocessors as the Motorola 6502 run at a clock rate of 1 Mhz, which means that a primitive processor operation such as_ fetching an instruction from RAM takes about one microsecond. Remote devices such as printers and disk drives, however, operate at very much slower speeds a dot matrix printer might take 3,000 microseconds to print one character. Transferring data between the computer and its peripherals, therefore, must be governed by strict protocol:

handshaking is an interlocking protocol by which

one device signals its readiness to receive or transmit a block of data, but does nothing until a corresponding ‘ready’ signal is sent by the other device. If transmissions are not controlled in some such way, data will be corrupted by the faster device reading the same data twice, or by the

slower device reading only the start of i incoming data.

Handshaking can be managed entirely

| software, but is reasonably easy to ‘hard-wire’ into

the device interfaces. The Motorola 6802 Peripheral Interface Adaptor (PIA), for example, communicates through its data register: when this is written to, a flag is automatically set in the PIA’s status register; reading the register then resets the flag. Handshaking with this facility is reasonably

Straightforward: the CPU sends a character to the

PIA, and continues with whatever program processes are current until it finds that the PIA’s read/write flag has flipped, indicating that the

external device has read the data register. This means that the CPU can send the next character to

the PIA. The state of this flag, then, shows the PIA’s state of readiness, and could be wired to one

of the chip’s pins to Serve as the veel Not Ready’ signal line.

HARD DISK

728 THE HOME COMPUTER ADVANCED COURSE

old-fashioned

An increasingly common alternative to the floppy

disk drive with its interchangeable slow-speed/

low capacity disk is the high-speed/high capacity hard disk. In this device the disk spins constantly at high speed in a sealed atmosphere sometimes a

vacuum, sometimes an inert gas such as nitrogen

and is never removed from the drive. The

storage capacity of the hard disk is therefore

considerably higher than that of a floppy disk, where the need for robustness, cheapness and convenience affects the engineering design and precision. Hard disk drives with between 10 and 30 Mbyte capacities, costing about the same as a modest business micro, are now freely available.

Managing the hard disk’s contents requires some care particularly the duplicating of system software and data files. Many owners ‘back-up’ the contents of the hard disk onto floppy disks

every day, so that ifa crash occurs, the floppies can

be used to reconstitute the system. Copying several Megabytes can take hours and many disks, however, so high-speed tape recorders (called ‘tape streamers’) are often used instead of floppy disks. Even so, new software must be loaded onto the hard disk somehow from a floppy disk drive or via a data link to another system. Either method increases the not inconsiderable cost of the device.

HASHING

When data domains are large but ome space is limited, some method of ‘Mapping the domain into the file record structure is necessary; hashing (see page 273) is a common method that combines efficient use of file space with reasonably high record access speeds. Let’s assume that records ina file are to be arranged alphabetically according to the word in the first field of the record, and suppose that the field is 10 characters long: it

_ would be very convenient if the ASCII characters of the word could be used to give the position in

the file of the record ‘A’ should go in Record 1, ‘B’ in Record 2, and so on. The number of possible combinations of 10 ASCII codes in the range 65 to

90 is enormous, however, and no file could

accommodate them. The solution is to ‘hash’ the name’s codes so as to produce a reasonably sized number. In this case, several different names will probably produce the same hash; when a record is to be stored but its hashed location is found to be occupied, the hash itself is rehashed to produce another possible location for the record.

HEADER

Data or programs are stored on tape or disk in files of various different formats (see page 124). The first information in the file, therefore, is written there by the system at file creation time, and consists of the file’s title, type and length. This is the file header. If you listen to a data cassette on an audio cassette player you will hear a high-pitched tone first (the synchronisation reference tone), followed by a short burst of data followed again by the reference tone; this first section of data is the file header.

HARDWARE/PERIPHERAL OVERVIEW

PERIPHERAL OVERVIEW/HARDWARE (Cy

SINE WRITING

IAN McKINNELL

732 THE HOME COMPUTER

In the last section of Workshop we built a digital-to-analogue converter to expand our user port system. We can now begin to design software to produce sound signals from this device. In this instalment we look at the production of different waveforms and discover how to determine the duration of a note.

Once these steps have been followed, we can test the system using a short Basic program. In essence, sound is generated electrically by providing an oscillating voltage to a speaker. We can generate a crude oscillating voltage output from our D/A converter by changing the contents of the user port data register from 0 to 255 and back in rapid succession. Type in the following program and RUN it. Turn the D/A potentiometer clockwise until sound can be heard.

Si i eee DDR=346579 2 DATE DDR, 255

KEKE

DATRES

ADVANCED COURSE

Notice that the Basic program has a repeating

structure, all crunched down onto a single line to produce maximum speed. There is a delay loop

inserted between the data register being set to 255

and it being set io 0. The value N in line 35 sets the

length of this delay. Try altering the value of N and re-running the program. You will notice that the pitch of the tone heard goes down as the value of N increases.

The highest pitch obtainable from this BAsIc program will occur when the delay loop is removed altogether. Even a loop executed once has an audible effect on the pitch of the note heard.

If you have experimented with different values of N in the BASIC program given, you will have noticed that changing the value of N by 1 hasa significant effect on the pitch of the note. BASIC is just not fast enough to allow us to control the rate of oscillation accurately. Instead we must use machine code.

In the next instalment of Workshop we shall look at the difficult problem of controlling pitch and volume from machine code. Here we concentrate on devising a program to produce different waveforms. The waveform produced by the BASIC program used earlier was a Square wave. It is, however, possible to produce other waveforms, which alter the ‘quality’ of the tone heard. We can digitally synthesise sine and saw- tooth waves by taking a number of samples of the waveform and putting them in a look-up table. The machine code program required to place these samples one after another in the data register is in essence very simple. Our illustration shows these three waveforms, together with accompanying look-up tables for sine and saw- tooth waves. If the waveform cycle is divided into steps, and these steps sampled, then the program loop that sends these samples out through the user port is shown left.

In producing sound, timing is crucial. Next to each instruction is the number of machine cycles required to execute that instruction. From this formula we can calculate the total number of machine cycles it takes to produce one complete waveform cycle: number of machine cycles = 2+(44+-4+242+3)x steps —1 = 1 + 15 X steps.

If the wave is split into 80 samples then the number of machine cycles required to produce one waveform cycle is 1,201. :

As each machine cycle in 6502 code takes about a millionth of a second, the total number of waveform cycles that can be produced in one second (i.e. the frequency of the note) is given by this calculation: frequency = 1,000,000/1201 = 832 Hz. As middle C is 512 Hz, the note produced

SELIG LEI OOS TS TIS IE RTI DB SSR ETT

ee

SASSER TATE USNS Tels yi

LIZ DIXON

“THE SAW-TOOTH WAVE

AMPLITUDE

-_THESQUARE W WAVE

Form Filling

The sine wave and the saw-tooth waveforms are Created by first deciding how many sample steps are to constitute one cycle of the wave. These samples of the wave’s amplitude are then calculated and stored in a look- up table. The values in this table can then be copied in sequence to the user-port data register

_ and thus to the digital-to-

analogue converter where they become voltage levels. The

advantage of the look-up table i is -

that it allows the time-

- consuming Calculations to be

done in advance; actually generating the waveform therefore takes little time, and

_ this makes a frequency range of _ several octaves possible.

Without the look-up table the range would be restricted to two octaves.

The square wave can be

generated by a BASIC program. because the processisso

simple. BASIC’s slowness, - however, restricts the frequency range considerably

~ LOOK: uP P TABLE

- GENERATING PROGRAM ,

sheild be aitchod just a a notes ager a than middle C.

It can be seen that the number of sample steps in which we choose to divide our waveform has a direct effect on the frequency of the final note. Doubling the number of sample steps would halve the final note frequency. Obviously, the more samples we make of the note the nearer we are likely to get to the tone quality we are synthesising, but this must always be weighed against the final maximum frequency that can be achieved for a given number of steps.

One waveform cycle is unlikely to be long

enough to be audible, so we must also include

code to repeat the waveform-generating section of code a set number of times. The number of repeats can be determined by setting a counter value and decrementing it to zero. To give a large range of counter values, a 16-bit number stored in two adjacent locations has been used. In addition to this code, interrupts are disabled at the start of the program by SE1 and re-enabled by CL1 at the end. If interrupts were to occur during execution, then this would make the timing of the program inaccurate. However, we can disable some interrupts only; non-maskable interrupts, if they occur during program execution, can still be the

cause of some timing errors. ©

The waveform data must be set up in : memory ‘ae : | asa look-up table, witheach waveformtypetaking —_— 80 consecutive locations. In the Commodore

version the sine wave look-up table is located in

memory starting at $C000; the saw-tooth tableisat $C050 and the square wave table starts at SCOAO. The machine code program is.designed to default _

to load data from the sine wave table using indexed addressing, but we can switch to another table by modifying the program directly with a POKE from sasic. The LDA part of LDA SINE,X is in location $C103. The start address of the table of | data to be loaded has its LO—byte in location $C104 and its HI-byte in location $C105. To modify the start address of the data to be loaded all we have to do is to change the number held in $C104.

Normally, for a sine wave, this location will hold 0;

if we wish to change to a saw-tooth wave then all

we need to do is change the contents of $C 104 to 80. Changing the contents of this location to 160 will change the waveform to a square wave. | The BBC version is also designed so that the look-up tables start at the beginning of a new page in memory. Because the tables start at a new page, - the HI-byte of each of the table start addresses is the same, and we need modify the LO-byte only. On a normally configured BBC Model B in mode 7, HIMEM is &7C00. By lowering the top of memory by three pages we allocate more than enough

space for the look-up tables and machine code

program. BEE EEE EEE EEE EET A a ate ee oe a ea ie ia ++ ;++ CBM 64 SOUND ++ pt+ GENERATOR ++ 5+ te 5 bE EE EEE EEE EEE EET AREER EEE EAE EEA EET PORT =. SGa77. ;DATA REGISTER GDDRESS STEFS = 30 ;NO. OF STEPS IN WAVE += $COO8 +++ SET UP DATA TABLEGREA. #+++ SINE ¥=K4+STERS SAW K=K4+STEPS SGUARE #=*+STEFS NUMBER #=*+2 COUNT . #=#42 p++++ MGIN PROGRAM ++++ | eo} ES ty Yat AD Fa ce LDG NUMBER. 15 . ee alee 3 SBD F2 ce SE GUN sSET COUNT VALUE 9 | AD Fi CoO - LDA NUMBER+1 SrA a has SD FR-co ' STH COUNT+1 LOOF2 AZ ae LDX ##0@ AL OOF . BD Ba CA LDA SINE,X :GET DATS BD @i DD STA FORT :FUSH TO USER FORT ES TNX E@ 5@ CFEX #STERS < D@ FS BNE LOOF? SEND GF ONE CYCLE s4444+ DECREMENT COLINT +444 AD F2 Co LDA COUNT 38 - SEC EF @1 SBC #481. 8D F2 Ce STA COUNT AD FS co LDA COUNT+1 7 oe SHC #80 8D FS ca STA COUNT+1 SG Ne De Ea BNE LOOF? 2IF HIBYTE?2 ~ Ao a2 LDA ##00 eae CD F2 Coe CMP. COUNT D@ D9 é BNE LOOFZ. 3IF LOBYTE:@ 58 2 ip bes

58 RTS

THE HOME COMPUTER ADVANCED COURSE 733.

4

The machine code can be entered into your

machine by typing

in the source listing provided

and assembling it to create a hex file that can be loaded whenever required. The look-up tables can be generated by running the following

99% GEM *2** CBM SOUND CALLING FROGRAM *#2##* oe ad er = Gin DH=G:REM FOR CASSETTE DM=1 92@ IFG4=@ THENG=1:LOG6D"SSOUND.HEX",Di',1 ToS 3: i@@@ REM **#* SET UF DATA VALUES #*x#® 1485 5=8@ :REM RUMBER OF STEPS 1@@7 TE=12*4094 : REM START OF DATA AREA 1i@@& : 1@i@ FEM #*¥SINE WAVE EF ive@ FOR I=@ TO 5-1 LARA Y=H=1SFS*SING XD +1 2S 1940 FORE TetI,y¥ 1@45 X=E+2/5 1@S@ NEXT f s i94@ : 4 1@45 REM **#* SAW WAVE #EHE 1@7@ Y=255:TR=TH+S i@sAa FOR [=@ TO S-1 1096 FORE TRtI,7 11@@ Y=Y¥-2S5/S Lita NEAT I M1285: 11235 FEM *®*** SGUARE WAVE ###*® 4150 Y=255: TR=THt+S ; 114@ FOR [=@ TQ S/e-1 LiSO PORE TRt+L,¥ 114@ NEXT I 1165 Y=@ 117@ FOR I=s8/2 TQ S-i1 118@ FORE TRtI,y¥ 1190 NEXT I LESS [AGO REM **** DISPLAY DATA TABLES ###* S005 TH=12*4a96 SiG FORI=TH TQ TR+i*Ss—-] 2@2@ FRINTI,I-TB ,PEEE (YT) 2@3@ NEXT

-After running this

program, type NEW and then

type this sample program that demonstrates how to use the machine code, giving the SYS and POKE

addresses required

to interact with the machine

code from sasic. This program asks the user to enter the wave type required and then produces a tone each time a key is pressed.

1@ BEM *#*# CBM 44 SOUND #kE®

oA REM «##** SAMPLE FROGRAM Hx#*

as

4% DDR=54579:F0KE DDR,255: REM ALL QUTFUT 65 CL=49292 :REM COUNTER LOBYTE LOCATION &? Th=49412 :REM TYFE LOBYTE LOCATION

7@ SQUMD=49294: REM FROGRAMN START ADDRESS 75 FEM *#* SET COUNTER VALUE ®*

GQ NUM=8@s NHI=INT ONUM/ 256) s NLO=NUA-2562NH I Q2 FPOKE CL,NLO:FORE CL+1,NHI

& FPRINTCHRS (147): REM

86 INFUT"WAVE TYFE (0 87 FOKE TL,WT*s

88 PRINT: PRINT"PRESS 9Q GETA#: IFA#="" THEN?

1949 SYS SGUND: REM CAL 4 11@ IF At="X" THEN &S 124 GOTG 7a

CLEAR SCREEN SINE (1)SAW (2) 5QUARE"s WT ANY KEY (RUN/STOF TO END)" @ :REM WAIT FOR FEY L. MACHINE CODE

If you do not have an assembler or do not understand Assembly language then you can still use the machine code program by typing in this BAsic loader and running it. In this case, you can omit line 920 from the program that sets up the

look-up table.

14 REM ***#* BASTC LOS 2A FEM #RE HAC SQ FOR T=49294 TO 494 40 READ A: FORE 1,4

a@ CC=CC+&

60 HET I

70 READ CS:1F ClesCs 10G

114 120 120 144 pan 4) 144 17a

DATAZ42,192,206,2

734 THE HOME COMPUTER ADVANCED COURSE

DEF FOR CEM SOUND #### HIME CODE KREBE 4S

THEN PRIHT"CHECESUM ERROR": END

DATAL ZO, 17%, 240,192,141 ,242,192 DATAL72%, 241,192,141,245,192,162,0 DATAIS9,@,192,141,1, 221,232, 224,90 DATG208 , 245,175,242,192,56,2335,1 DATG141, 242,192,173, 243,192,222,8 DATA1I41 , 243,192, 208,224,167,0,205

17,88,96

DGATA97115: REM*#CHECESUM*

FOR THE BBC | eo

As the BBC has its own built-in assembler, the process of combining Basic with machine code is substantially easier than on the Commodore 64.

1198 1198 1200 1220 123a 1240 1250 1268 12778 128a 1290 13519 1320 1230 124@ 1350 156 1270 128a 1=90 1428@ 1418 1428 1438 144@ 1450

1455

1464 148@ =aae 2028 2825 2828 [04a 2850 =46@ =07@ 2a98 =100 =11@ 212a 2128 2=14@ 2148 2g.

2180 2170 228

2228

bah Ei led pret are)

22409 SEaa 2278 2288 2298 25ae 2218 PaaG S800 28208 2030 2048 S50 S050 2a7@ 2088 ZATA . 2108 211a 21208 ies S124 S144 S150 2140

REM *#** BEC SOUND FRUGRAM #*®**

MODE 7

HIMEM=HIMEM-£@2@1

MCZ=HIMEM+1

DDR=2FEG2: 7 DDR=255:REM ALL GUTFUT

por t=£FE4@: REM USER PORT CATA REG steps=6@ :REM NO. GF STEPS IN & WAVE table start=MC%

FPROCset up tables

FROCmachine code

FROCsample program

END i

DEF FROCmachine code

FOR apt#=1 TO 3 STEF 3 PH=MCA

sine=F%: PX=P4t+steps saw=F75 FA=P4t+steps

@ square=P“4:F%=FPet+steps

number=F:FPA=F4+2

count=F4%: FPR=PA+2

L

QFT ant’

.**#* MGIN FROGRAM STARTS HERE

.50und

HEE

SEI LDA number STA count LDA numbert+i STA countt+l .laape LDX #286 -loopl g LDA sine,X STA port INX CFX #steps BNE ioopti \##e* DECREMENT COUNT ###* Ni LDA count SEC SBC #241 STA count LDA cauntt+1 SEC #2.0@8 STA count+t+i BNE loape LDA #208 CMF count BNE loops (ia et ETS q NEXT opt ENDFROC

DEF FROCset_up tables REM *#*** SINE WAVE #### x=6

FOR I=@ TQ steps-i yHl27eSIN(x) +127 ?i(table_startt+Ii=y H=xn+24F1T/steps

NEXT I ee REM **** SG WAVE ##e#

y=255: table start=tabie starttsteps FOR [=@ TQ steps-i ?itable_start+tl)=y

y=y-205/steps © NEXT I REM **#* SGUARE WAVE *##*

y=255: table start=table_startt+steps

FOR [=@ TQ steps/2-i

?P(table startt+I)=y

NEXT I

y=@

FOR T=steps/2 TO steps-1

Pitable startt+I)=y

NEXT I

REM **** DISPLAY DATA TABLES #***

table start=MC%

FOR I=table_ start TO table start+3*steps-—1 FRINT “1I,™*(I-table_start),? I

NEXT I

ENDFROC

DEF FROCsample_program counter=MC%+3*steps: REM COUNTER LOBYTE LOCATION type=loopi+i: REM TYFE LOBYTE LOCATION caunt value=8A

count Hi=count value DIV 256é

count lo=count value MOD 256

Pcaunter=count_ loa

caunter ?l=count_ hi

CES

INFUT"WAVE TYPE (@) SINE (1) SAW (2) SQUARE" pwave Tilype-wavertsteps

REPEAT

FRINT“FRESS QNY EEY (xX TO EXIT)"

AL=-GETS

CALL sound UNTIL Aft="xk" BOTS sac

ee a a eminem

Ny ea eR

ease

n this instalment of our LOGO course, Ww look at the facilities the language offers for working with numbers. Loco would probably not be the first choice of language for applications that require a lot of calculation, but it does offer an impressive array of numerical primitives.

Almost all Loco implementations support both integer and real (decimal) arithmetic, using the infix operators + - * / . These operators are called ‘infix’ because they are written between the numbers they work on —for example, 3+4. Some Locos also include ‘prefix’ arithmetic, in which our example would be written as SUM 3 4. One advantage of this notation is that it is consistent with the way in which other Loco operations and commands are written.

MIT Loco supports infix arithmetic only, but it is simple to program prefix forms if they are required. Define SUM and PRODUCT and try them:

TO SUM :A:B

OUTPUT :A+ :B

END

TO PRODUCT :A:B - OUTPUT :A * :B

END The ‘precedence’ of operations (the order in which they are carried out) follows the usual mathematical rules. Anything within brackets is done first, followed by multiplications and divisions, and finally additions and subtractions:

PRINT (3 + 4) *5 PRINT3+4*5

Now try the prefix forms:

PRINT PRODUCT 5 SUM 3 4 PRINT SUM 3 PRODUCT 4 5

This demonstrates another advantage of the prefix forms there is no need for rules of precedence and the line is evaluated in the same way as any other line of LoGo commands. :

The usual division operation (/) gives the result as a real number. Two other operations, QUOTIENT and REMAINDER, are often useful for working with integers.

QUOTIENT 47 5 is 9 REMAINDER 47 95 is 2

A standard method for converting a number in |

base 10 to binary is to keep dividing the number by two until the result is zero. The binary number is found by writing the remainders found at each

FIGURE IT OUT

stage in reverse order. For example, to convert 12 to binary:

12/2 = 6; remainder = 0 6/2 = 3; remainder = 0 3/2 = 1; remainder = 1 1/2 = 0: remainder = 1

So, reading the remainders upwards, we find that decimal 12 is 1100 in binary.

Using QUOTIENT and REMAINDER we can implement this technique easily in Loco. By putting the print statement after the recursive call we get the remainders printed in the correct (reverse) order. 7

TOBIN :X IF :X = 0 THEN STOP BIN QUOTIENT :X 2 PRINT1 REMAINDER :X 2 END Two operations exist for rounding numbers INTEGER and ROUND. INTEGER outputs the whole number part of a number, simply ignoring any figure after the decimal point, and ROUND rounds a

number up or down to the nearest whole number.

The following procedures calculate the compound interest on an investment at a given rate of interest. In PRETTY.PRINT, INTEGER is used to get the pounds, and ROUND is used to round the pennies to the nearest whole number.

TO COMPOUND :PRINCIPAL :RATE :YEARS IF :YEARS = 0 THEN PRETTY.PRINT ‘PRINCIPAL STOP COMPOUND :PRINCIPAL * (1+ :RATE/ 100 ) “RATE :YEARS 1 | END

TO PRETTY.PRINT : MONEY MAKE “POUNDS INTEGER :MONEY MAKE “PENCE ROUND ( : MONEY :POUNDS ) * 100 : (PRINT :POUNDS “POUNDS :PENCE “PENCE) END.*

TESTING TIME

We have already used =, <, and > as logical tests in anumber of procedures. The logical operations ALLOF, ANYOF and NOT can be used to combine other tests. ALLOF is true if both its inputs are true, ANYOF is true if either of its inputs is true, and NOT is true if its input is false. So we get:

IF ANYOF :X > 0:X=0 THEN PRINT “POSITIVE

IF NOT :X <0 THEN PRINT “POSITIVE

IF ALLOF :X > 0:X < 100 THEN PRINT [BETWEEN 0 AND 100]

THE HOME COMPUTER ADVANCED COURSE 735

LISSAJOUS FIGURES

ee

One Step Over The Line

The Drunkard’s Walk theorem states that after N steps in completely random directions the probability is better than 0.5 that the drunkard’s distance from the starting place will be less than SQR(N) steps. This is a statistical prediction based on a large number of steps, LOGO lets you test it for yourself: TO DRUNKWALK :STEPNO ‘STEP CS REPEAT :STEPNO [RT (RANDOM 361) FD. STEP] : END

DRUNKARD’S WALK

STEVE MALONE

ct ERE cst serra ee : HEHE enn ele eet i EEE Be aE ti

: : . : 2) a : : ne a ____sé=iouimgdumwdwléititsw

i ditt fete fh Hn _. :

The operation NUMBER? outputs TRUE if the input is a number, otherwise FALSE is returned. We use this in the procedure PRIME?, which outputs TRUE if its input is a prime, and FALSE otherwise. It begins by checking that the input is indeed a number, and that it is greater than two. PRIME.TEST then checks to see if any integer between the square root of the number and two will divide into it exactly, leaving no remainder.

TO PRIME? :NO IF NOT NUMBER? :NO THEN PRINT [NOTA NUMBER DUMMY] STOP IF :NO < 2 THEN OUTPUT “FALSE OUTPUT PRIME.TEST :NO INTEGER SQRT :NO END |

TO PRIME.TEST :NO :FACT IF FACT = 1 THEN OUTPUT “TRUE IF (REMAINDER :NO :FACT ) = 0 THEN OUTPUT “FALSE OUTPUT PRIME.TEST :NO :FACT 1 END

RANDOM NUMBERS

RANDOM n outputs a random integer between 0 and n-1. The procedure DRUNK makes the turtle stagger across the screen, turning a random angle at each step. The input A gives the maximum size of the turn that can be made at any time. If you run this procedure you will find that the turtle turns in vague circles, moving to the left or to the right depending on the value assigned to A.

TO DRUNK :A FORWARD 1 RIGHT (— :A/2 + RANDOM :A) DRUNK:A

END

or

736 THE HOME COMPUTER ADVANCED COURSE

a a : _. a He _

Pi COMES TO MONTE CARLO

The so-called ‘Monte Carlo method’ is a technique for solving mathematical problems through the use of random numbers.

We'll demonstrate by finding an approximation to pi by using this method. Our illustration shows a quarter-circle drawn within a square. The area of the square is 100 X 100 square units, and the area of the quarter-circle is (1+4) X pi X 100 x 100 square units. The ratio of the areas circle + square is equal to pi + 4. Now drop a pin at random on the square 1,000 times and count how many times the pin falls within the quarter-circle; call this number IN. The value of IN/1000 should be approximately the same as the result of: circle + square i.e. pi + 4. So if we do the experiment, multiply IN by four and divide by 1,000, then the result should be an approximation to pi. That is precisely what the following procedures do:

MAKE “IN 0

MC1 1000 100 100 ,

(PRINT [VALUE OF PI 1S] 0.004 * (:IN) ) END. .

TO MC1 :NO :XNO :YNO IF :NO=0 THEN STOP RANDOM.POINT :XNO :YNO IF INSIDE? THEN MAKE “IN :IN + 1 MC1 :NO 1 :XNO :YNO

END

The procedure MC simply sets the conditions, calls MC1 and prints the results. MC1 does most of the work, calls RANDOM.POINT to position the turtle, and then increments IN if the point is inside the circle. This continues until the procedure has been carried out the correct number of times.

TO RANDOM.POINT :XNO :YNO SETXY RANDOM :XNO RANDOM :YNO END

TOINSIDE? | IF (XCOR * XCOR + YCOR * YCOR ) < 10000 THEN OUTPUT “TRUE OUTPUT “FALSE END

RANDOM.POINT sets the turtle at a random position within the square, while INSIDE? checks to see if the turtle lies within the circle. It will take some time to run this, but eventually a value for pi of 3.15999 will be obtained.

a

ee ye ne EE EEE ee

Lissajous curves are an interesting family of curves in which the x co-ordinate of each point is determined by the sine function and the y co- ordinate by the cosine: |

Logo Exercises 1. Write a procedure to output the nth power of a number, so POWER 4 2 would output 16.

2. Write a set of procedures to convert a decimal number to hexadecimal (use a similar technique to the binary example, but this time divide by 16).

3. Write a procedure EVEN? that will output TRUE it a number is even and FALSE if itis not. 4 Use the Monte Carlo method to find the area under the curve y=x° between x=0 and x=10.

THE HOME COMPUTER

LISSAJOUS FIGURES

ADVANCED COURSE 737

DESIGN

CODING

‘Seeemeene’inacomtter’ pen ones: Someeuennn = tememnency 9

TESTING

MAINTENANCE

Design Counts

Observing the rules of good structure is difficult in machine code programming. Developing machine code programs according to the rules of good design is not difficult, however, and pays extra dividends in clarity of design and debugging time saved

In the course So o far, we have ponccnanted on looking at the 6809's instruction set and seeing how a few of these instructions can be put together to form simple routines. However, writing larger, more ambitious programs is a far more complex task. We consider some techniques to give structure to larger Assembly language programs.

We have talked a lot in the course about ae

benefits of proper program design, modular construction and structured programming in the

context of high-level languages. The difficulties of programming, and the benefits of good technique, are greatly magnified at the lower level. In Assembly language, there are usually no convenient control structures, such as BASIC’S WHILE... WEND and IF...THEN...ELSE, to enforce at least some sort of structure on the code. There are also no convenient notations, no data- typing of variables, and, to make it worse, you can expect an Assembly language program to be between six and ten times the size of a high-level program in terms of the number of instructions. Above all, it is far easier to make errors, and these may have disastrous consequences it is possible to wipe out all the data on a disk with an error ina single byte. To help make 6809 Assembly language programming less daunting, we consider here the most productive way to approach it. Theres nothing particularly new about structured programming or software engineering: experienced programmers have always known that forethought and clarity of approach were the ground rules for a successful programming style. What makes it seem new and original is the fact

_ that the world of microcomputing has been largely

amateur and hobbyist, but it is now becoming both more professional and more appreciative of the professional virtures. Nothing makes this point more clearly or memorably than your first attempt at debugging an undocumented, unstructured, hand-assembled machine code program that you created months ago and put aside. Good design and working methods mean good programming.

STAGES IN PROGRAM DESIGN

@ Problem Specification: In this stage, the Assembly language programmer must pay particular attention to the specification of input and output. Often peripheral devices are being controlled directly especially the keyboard and screen so the actual signals used must be considered. There may be timing constraints as well. You may not have any convenient routines

738 THE HOME COMPUTER ADVANCED COURSE

available that convert the string of bytes that come in or go out into the form in which the program reads the data for example, converting a string of ASCII characters into a decimal number in binary form. It is important, therefore, to specify not only the form in which the data arises but also the form in which it is required by the rest of the program.

e@ Program Design: We must now consider the processes that will turn the program’s specified input into its specified output. These should be grouped where possible into logically self- contained modules, along with the data that each process requires. There are two main techniques for ‘decomposing’ a program into modules: bottom-up, where you collect a set of what would appear to be useful modules in the context of the program and then try to fit them together; and fop- down, where the program is_ successively decomposed into smaller and smaller units, concentrating on the function of each unit rather than how it is to be achieved, until the process cannot usefully be continued. Only at that point do you start considering how each module can be assembled into code.

Bottom-up design has the great advantage of using library modules, which are easy to put together, and the end result is likely to be more efficient in memory usage. The disadvantages are that the program as a whole is likely to prove more difficult to debug and test, and will not be so comprehensible. ‘Top-down design leads to better structured programs, and each stage in the process can be tested separately by means of ‘stubs’, which are short routines that take the place of as yet unwritten modules by simply accepting input and providing output in the correct form without doing any processing. The disadvantages are that the programs will tend to use more memory and the routines developed are unlikely to have any immediate use elsewhere. |

Within each module the data requirements, data structures and algorithms must be specified. A flowchart is useful at this level for representing algorithms, but many people find it much easier to work in a loose kind of high-level language called a pseudo-code. PASCAL is usually used as the basis for this pseudo-code, but there is no reason why BASIC cannot be used. This enables us to design algorithms and data in a way that is familiar to us, and confines the lower-level work to the relatively simple task of translating the algorithm from pseudo-code into Assembly language. This is

-much easier than trying to design and code in

Assembly language at the same time. @ Coding: If the routines have been well designed

| |

i

then this stage will probably be the easiest and least time-consuming of all. In order to translate from a high-level algorithm to low-level code it is essential that the control structures used at high level are carried over to the low level, avoiding the temptation to use BRA and JMP indiscriminately. Remember that any time you save by writing unstructured code is certain to be ‘clawed back’ in a frustrating trial-and-error debugging stage. In the diagram we give some examples of the way in which the common control structures can be coded assuming, for simplicity, that the data items used are eight-bit.

One problem with coding with control structures in this way is that the program is longer than it might be. Where space is not limited then there is no point in trying to save it; short code does not usually mean shorter running times but it does mean longer development and debugging times. Where space is limited, then it is better to write ina spacious structured way, and introduce a further stage of optimisation where the working code can be shortened to take into account particular circumstances, retaining as far as possible the essential structure. ® Debugging: At this stage, each module is separately tested using stubs where necessary to make sure it gives appropriate outputs for valid inputs. Debugging Assembly language programs differs considerably from BASIC program debugging. To be able to see what is happening, it is necessary to be able to inspect the contents of the registers and the memory locations used by the program, and to change them if necessary. It is nearly impossible to debug an Assembly program without the use of a utility for setting and removing breakpoints. These enable you to run the program up to the next breakpoint, then dump the registers, and inspect and change memory contents. ® Testing: Once each module has been tested and debugged then the entire program has to be put together and tested with appropriate data. ‘This is much easier when you know that all the component parts are working properly. ® Documentation: Assembly language programs are more difficult to understand than high-level programs, so documentation is even more important. In particular, it is vital to document the use of memory, the use of the stack (especially while passing parameters), and the register usage within subroutines. ® Maintenance: If a program is to be used over a period of time then at some point it will probably need revision either to remove any bugs that appear or to make improvements. It is at this stage that time spent in careful design and documentation really pays off. If the program is badly designed and/or poorly documented then you are better off doing a complete rewrite rather than attempting to make alterations.

Now we need a project to apply these design skills to: for our first venture in structured Assembly language programming nothing could be more appropriate than a machine code

monitor/debugger. If you’ve used an assembler before, then you may be familiar with the kind of utilities to expect from a monitor/debugger. Essentially, it gives the machine code programmer

the kind of editing facilities that the BAsIc

programmer takes for granted namely, the ability to inspect and change the contents of memory. |

In the next instalment of the course we will take this project through the design and development stages described in this article, to create an important and extremely useful programming aid.

Basic Backbone

There are no control structures written into Assembly language, so it pays to import tried and - tested methods from high-level languages. The structures shown here are clear and graceful in both high- and low- level languages, and should be uséd to the exclusion of all

~ alternatives ;

THE HOME COMPUTER ADVANCED COURSE 739

IAN McKINNELL

The Game Progresses

Deus Ex Machina can be either played or viewed as an entertainment. There is a wide variety of screens in the game, although some do bear a resemblance to others. The tactics required to maintain the ‘ideal entity’ are changed constantly. The score is shown as a percentage in the bottom right-hand corner and slowly falls as the game and the defect’s life progresses

THE DEFECT EFFECT -

Aut offer ‘a completely new form of computer entertainment’. Combining elements of well-known arcade games with an audio soundtrack featuring showbusiness stars, this complex program allows you to take the leading role in a ‘fully animated televised fantasy’

As computer games have developed into a major part of the leisure industry, it was perhaps inevitable that software houses would join forces

with other segments of the entertainment

business. Automata Software, best known for its series of games featuring the Piman, has taken the first steps in this direction by developing a product that contains not only computer software but also an audio cassette that can be synchronised with the

computer program to provide a soundtrack to the game. This soundtrack features well-known

figures like Jon Pertwee and Frankie Howerd. The idea behind Deus Ex Machina, which took

‘six months to develop and three months to program, is that an all-powerful computer of the

future rebels and assists in the creation of a human ‘defect’. The player, as the defect, passes through various stages in the game that depict experiences from childhood to old age. The player’s

- involvement begins at conception by guiding the

sperm towards the egg. As the child grows, it is under constant attack by the “defect police’. Itis up to the player to deflect these attacks, using either the keyboard or the joystick. Scoring is achieved by maintaining the ‘ideal entity percentage’, which begins at 99 per cent and drops under the assaults of the defect police. As the defect grows to adulthood, the nature of these attacks changes and the player must adapt to meet them.

740 THE HOME COMPUTER ADVANCED COURSE

Once the game is loaded, the audio soundtrack should be synchronised with the program. Care must be taken when this is done, as the various screens are timed to coincide precisely with the words and music, and this adds greatly to the enjoyment of the package. |

The program is divided into two segments, one half on each side of the cassette, making up a total of 96 Kbytes of code. At the end of side one, after an amorous scene in which the player must move a cursor around the body to meet the kisses drifting towards it, the second side must be loaded. ‘The computer should not be switched off, and again care must be taken to synchronise the soundtrack correctly. Player involvement in the second half consists mainly in jumping over obstacles before reaching ‘old age’. At this point, large blocd clots appear onscreen, which must be broken up by the player. At the end of the game, no matter what the score, the defect dies.

Deus Ex Machina is unusual in that there is no

- winning score, and in fact the player need not even

participate in the game at all. Events will unfold in the same way without any participation, so you have the choice of becoming involved in the game or sitting back and watching it as a piece of entertainment. The graphics are uniformly excellent and imaginative. Although none of the screens is breathtaking, they do reflect the care and attention to detail devoted to the whole package.

The soundtrack music was written and performed entirely by Automata’s co-founder Mel Croucher, who also wrote the story. The songs themselves are pleasant but not exceptional. The best number accompanies the scene in which the defect comes to life, and is sung by Ian Dury.

The, story and soundtrack are quite different from most computer games and reflect the non- violent philosophy behind all Automata’s computer games. Games enthusiasts who enjoy destroying fast-moving barrages of attacking aliens would probably be disappointed, and many people may find the semi-mystical content of the lyrics irritating. However, Automata should be heartily applauded for their innovative idea. The program is a bold experiment and will no doubt be considered an important step in the development of computerised entertainment.

¥ 4 * i § { f i i % ¥ tt

«gespac a aa anc A RA SEE EEG DSTATDIDAUB ee

DATABASE

Here, courtesy of Zilog Inc., we produce another part of the Z80 programmers’ reference card.

16-Bit Arithmetic Group

SOURCE

DESTINATION

cn N.C :

Symbolic Flags GO) oforelel=) No.of No.of M No.of T Mnemonic Operation mo H PIV N C 76 543 210 Hex Bytes Cycies States Comments POD HL ss ea. a ce lr ses—sSSsC i D0 Ss) Ou : S ce SS eg Oo BO. ROC al ss HL HL+s5s4+CyY } f kK KF Ke YhUhUmUmLUCU Fe ED : 4 ce bbe O11 ssi O10 . ee oo FL oss ao et os OY | a © eOKLULDCLUCUC ! 1 Oo ED 2 4 ls OS) sso 66 : ADD IX, pp A 6S > =| FF FF F thmhmUmUl 1 DD 4 Le pp Reg. O pp) oO) Do Be Oi oe : ix 7 Se ADD IY, tr oe = - . . .lhrLLhLC .. Po 8 4 15 1 eG . OO BL 6 es 10 OU to Be Ne Ss So. ss *_ ©*§ § © © © © ee soo 6 : 1 6 ie ys Ll * ©* «§ = © hs oe DD 2 . 10 00 100 011 23 INC LY ly |1¥ + 1 ~— .- - . 2... 1 FD & 2 10 OO 0D ce Bae SS ss ss-] .- 5 . . . . . Oe ss) 6 : : 6 Deo x IX X-1 -— . - . . . .. 7 DO. 2 10 O00 60) Ot 26

DEC Ty = - . fs lhl; .. FD eC . 10

N@ees So sce) 6) ge se ess se Be gk oe ee 6 6 fac cei sel 6 Ba 8 oe ris any of the regisie’ pas BU DE LY SP

ie cle Nec ee e i

flag 9Ci aliecied © = [ag ese! = = jag sel *% [a0 5 Unknow) Wee > 2 cele seers ae © = 25 1 8) ge cen ee

ey