A PHILOSOPHICAL DIGRESSION
After the lengthy Chapter 9, the brevity of this one may come as a welcome relief. The brevity exemplifies the purpose of this chapter, which might be summarized as advising you not to send a man to do a boy’s job.
You might be tempted, after following me through 9 chapters, to say something like “Look, this seems to be an awfully complicated way to add 2+2. I can do the job faster in BASIC, or even with pencil and paper.” I could not offer as a valid defense the fact that not all numbers are so easy to handle. BASIC would still do the job just as well. Sure, once we get the program written, the machine code will run in microseconds, compared with the milliseconds required by BASIC; but what does that matter, when the speed of your fingers and the response of your eye are measured in large fractions of a second?
For controlling machines, the jump from milliseconds to microseconds might justify the extra effort. The utility programs that have been given at the top of each chapter served as a reminder that there are things–many things–that machine code does better than BASIC can. But not everything. If your operations are too fast for the eye to follow, is the program improved by making them MUCH too fast for the eye to follow?
Of course, the miniature arithmetic programs we have presented so far were designed to illustrate principles, rather than to do something useful. (“What?” I hear you cry. “Isn’t education useful in itself?” To which I reply, “Don’t be a smart alec!”) But here’s a pseudo-practical case, and one that’s not too different from some routines I’ve seen published in popular magazines.
I’m not going to give mnemonics for this program. You can work them out for yourself, if you want to. Here’s a hint that may save you a bit of time–the first 19 bytes are data. You might have guessed as much, from the fact that we USR our way into the program at 16533, rather than the usual 16514.
After you RUN the program, enter RAND (not PRINT) USR 16533. It’s a rather roundabout way to write a love note, even if my wife did think it was rather cute.
10-2
100 LET P$="39.38.56.46.49.0.49.52.59.42.56.0.47.52.40.42.49.62.51."
101 LET P$=P$+"17.130.64.1.19.0.205.107.11.201."
The point is, as I remarked at the head of this chapter, it’s not always wise to send a man to do a boy’s job. Don’t become so enthralled with your ability to write machine code programs that you overlook some easier, or more reliable, way to do what you want. If you have the choice between simple and complicated, simple is usually the more elegant. It’s often a poor bargain to trade many minutes of machine code writing for a saving of a few milliseconds in program execution.
But of course, if you want to do it just for fun…
There are jobs that machine code is best for. There are some jobs that machine code can do easily, but are hardly possible (or impossibly hard) for BASIC. Use your machine code for them. Usually these are tasks involving loops, or great numbers of repetitive calculations.
PRINT is often better done in BASIC. Loops are always faster in machine code. Arithmetic and trigonometric calculations tend to be easier in BASIC.
As a matter of general principle, you’ll probably find it best to block diagram your programs in BASIC before you start writing machine code. Usually you’ll get a feeling right there whether it’s better to stick with BASIC or to use machine code.
And that’s the end of a short, but important, chapter. There will be more.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.