Monday, August 24, 2015

Chief Execututor

  This is another program that does little more than allow you to move a cursor around a screen. At least the screen has some Print Shop images of the presidents -- and there are two screens of them.

  I was learning how to use tools better; tools that were written by Jeff or Scott or another assembly programmer who would send them in to us. I also was learning the architecture of the C-64 and the Read It below shows how the program is laid out in memory. There are 256 pages in a C-6 and each page has 256 bytes, give or take a few. Organizing all the tools and data can be challenging. Don't be surprised if I lay out "memory maps" in future Read Its. One of the main goals of LOADSTAR was to show readers how to program in BASIC.



Graphics by Paul Arsenault
  We all know what George Washington and Abraham Lincoln looked like — we carry pictures of them around in our wallets or purses. Some of us even know what Ulysses S. Grant looked like. And try as we may, we’ll never forget Nixon, Carter, Ford, Reagan and Bush, since they show up on the tube from time to time. But what about the dashing Millard Fillmore, or that handsome devil, Warren G. Harding? Well, thanks to Paul Arsenault and a host of LOADSTAR routines, we can learn to recognize all of the forty Presidents of the United States. It may come in handy should you ever find a picture of Grover Cleveland in your pocket.
The program is straightforward. There are two hi-res screens with portraits of the presidents on them, in the order in which they served. Imagine them as side by side, ten across and four down. If you move “off” the screen, you’ll go to the other picture. Move the cursor from face to face and press RETURN to see some information about the man under the cursor. If you want the president’s name displayed as you move around you can press SPACE. Press SPACE again to turn off the name display.
The information that’s shown when you press RETURN includes the president’s name, birth and death dates, years of service, state, party, wife’s maiden name and a rather dubious nickname. I used the 1989 Information Please Almanac for most of the data. As a journalist, I’m afraid I cannot reveal the source of the nicknames. 2015 hint: Knees Calhoon.
After you have studied the information a bit, you may want to test yourself. Press T and you’ll go to the test mode. Here you’ll be asked to choose a particular president according to the information given. The president’s wife’s name, his state, or his nickname will be shown. You then go back to the presidents’ screens where you are to try to pick the president. Try to answer ten correctly before you answer ten wrong. You may press Q at anytime to exit the test mode. You can tell if you’re in the test mode — there’ll be a little “T” in the bottom right corner of the screen.
The program uses Dave Johannsen’s MR JOYSTICK (from LOADSTAR #76) so you can use a joystick in either port rather than the cursor keys. Press the firebutton in place of RETURN.
Press Q to return to LOADSTAR.
That’s all there is to it. It’s no big deal. I found it to be a learning experience putting this simple program together, not only because I learned what the presidents looked like, but because of the many ML routines I had to juggle to get the program to work.
Look at lines 20 - 102 where I load in all of the stuff. The important numbers are POKEd into 782. These are the high bytes of the locations of each of the ML routines. A tip for BASIC programmers who want to use some of the great ML routines we’ve published — whenever possible, keep the low byte at zero. If you think in hex (and you should), this means that you’re using addresses like $9C00, $C400, etc. The last two digits are 00.
Here is a memory map of the program’s routines, in terms of the high bytes. You can think of these as “pages” in a 256-page book.

003-004       INPUT ANY.O
004-008       Text screen
008-139       BASIC area
139-140       Cursor sprites for pic 2
140-144       Color memory for pic 2
157-160       MR JOYSTICK
160-192       Bit map for pic 2
192-196       EXEC FONT
196-200       SCRIPTER
200-201       Cursor sprites for pic 1
201-203       UNPACKER.89
204-208       Color memory for pic 1
224-256       Bit map for pic 1

And here’s a thumbnail description of each routine. For more details, you’ll have to read the ZERO PAGE article that introduced each one.
INPUT ANY.O - Reads a PETASCII file and puts it into strings for easy BASIC handling. The cassette buffer doesn’t begin at the beginning of a “page”. The high byte is 3 and the low byte is 60. In hex, $033C.
MR JOYSTICK - Allows me to use two simple SYSes to activate the joystick. It saves a LOT of code, and works better than BASIC.
EXEC FONT - This is only half of a font. The reversed half of the font isn’t needed by SCRIPTER.
SCRIPTER - This great routine allows me to print on a hi-res screen. Check out BITS on this issue for details.
UNPACKER.89 - Takes a packed hi-res (or multi-color) picture from disk and displays it with only three POKEs and a SYS. See line 40 for pic1 and line 60 for pic 2. This also saves a lot of code and headaches. Each packed file was loaded into 100 - 130 and unpacked into the locations above. Then the 100 area can be used by BASIC — its work is done.
I’ve never worked with hi-res screens much before and I found that by using the tools we’ve published, it wasn’t too difficult. I also developed an appreciation for the cleverness of C-64 programmers who program games and other graphic-intensive programs in the relatively small 64K we have to work with.
Whenever I look at IBM disks with their programs that take up 256K+ (and don’t really do more than our programs) I get the feeling that maybe some IBM programmers have gotten lazy and extravagant in their techniques. Or maybe it’s just the nature of the IBM beast.
Give me a C-64 with its built-in BASIC and ML tools, anytime.

Back here in 2015: Tomorrow's program doesn't do much more than this one but for those of us born in the 40s it might bring back a memory or two of a little road we called ROUTE 66.
 

Saturday, August 22, 2015

Prescription Translator

  I promise the programs will get interesting sooner or later. This one is a simple menu of abbreviations and terms you might find on a prescription. If you aren't sure what is being said between your doctor and your pharmacist, it could come in handy. I think I might have written the program as an example of how to do a pre-mouse menu.


 I usually try out these programs in VICE before supplying you with the .d81 file that has that issue's programs on it. But I couldn't get the Prescription Translator to work from the .d81 file. So I tried SnapShotting the file and it worked with VICE. If you use VICE, download the .vsf file and attach it as a SnapShot file.
  Here is the Read It from 1990, pointing out the fact that I knew I was 100% in charge, since no one in the company ever looked at LOADSTAR. I could say whatever I wanted!

The powers-that-be here at SOFTDISK had a big meeting the other day to discuss spelling checkers. They asked me which one I used and I answered, “None. Can’t you tell?” Of course they couldn’t know, since no one here ever reads LOADSTAR except Jeff, Scott and me.
My answer was sort of facetious since I consider myself a pretty good speller and I don’t think too many typos get past us. (My most embarrassing error was on a recent LOADSTAR 128 disk where I spelled the French word “voila” as “viola”. My face is properly red, but then even the most sophisticated spell checker in the world wouldn’t have caught that one!)
Last issue a few of my editorial comments bordered on being anti-progress and a few of the beta testers pointed this out, so let me make it clear that I consider spell checkers a great boon for mankind. If I had one that worked with Edstar, LOADSTAR’s pet text editor, I’d probably use it.
However, spell checking doesn’t go far enough yet. I’m waiting for the next leap forward, the fact checker. Maybe when CD ROMs take over and complete encyclopedias are put on one CD, your computer will be able to check your writing for factual errors. Some of us may be shocked to see a whole screen full of reversed words, or worse, maybe the whole document will be reversed!
The first time I ever heard of a fact checker was in Jay McInerney’s book, “Bright Lights, Big City”. Apparently the big guys in the publishing field (THE NEW YORKER, SATURDAY REVIEW, even TIME!) hire Michael J. Foxish yuppies to check every sentence published in their magazines for accuracy. This definitely sounds like a job for a computer.
In order to keep LOADSTAR abreast of the big guys I splurged and bought a book called “The New York Public Library DESK REFERENCE”. It was cheaper than a yuppie. It’s supposed to have everything a writer needs to keep his drivel factual, if not interesting. I haven’t been through it all yet, but it looks pretty good. One of the charts had some information that I found quite useful, and maybe you will, too. I’ve written it up in a Run It program for this Diskovery. It’s a program that translates the cryptic prescriptions that doctors write to pharmacists. You see, it’s not only bad handwriting that distinguishes doctor’s writing; there’s also a code involved.
A disclaimer: the information in the Run It file is strictly that — information. I offer no medical advice whatsoever. I just happen to like knowing what my doctor is saying about me to my pharmacist.
Of course, you can always look at my code to see how easy it is to do menus of this sort. I use SCREEN SWITCHER 1990 to simulate windows and allow the menu to pop back in place, without having to be rePRINTed. The custom character set makes possible the little graphic symbols that doctors use.
Run It in good health!
 


  2015 here. I'm afraid tomorrow night is another woofer of a program. It's called Chief Execututor and it's mainly a way to show off some Print Shop images of the US presidents that we had.

Friday, August 21, 2015

Magic Squares

For some reason almost two years went by at LOADSTAR before I had another program (on LOADSTAR #72). It might have been LOADSTAR 128 taking up my time; it might have been that I was learning how to program better and use the programming tools we were publishing. Jeff Jones and Scott Resh had come on board and Jeff was teaching himself assembly language, which Scott was a wizard at. I remained the BASIC programmer.

 
  I just looked at the program and it asks if you want to generate some 5x5 magic squares or read a tutorial that explains how they're made. The Read It below gives you an algorithm for making 5x5 squares, but it's completely different from the one used by the program. I don't know why I did it that way, but you get two algorithms for the price of one. The staircase method (below) is probably the easiest to remember and dazzle your friends with. Ask them to make a 5x5 square where every row, column and diagonal adds up to the same number and it's doubtful they'll be able to do it. But memorize the sequence below and you can come up with as many as you want while they watch.
 
  Here is the Read It from 1990.
 


  I remember the first time I heard of Magic Squares and how they’re made. It was in a child’s puzzle book and the problem was to take the numbers from 1 through 9 and place them on a Tic-Tac-Toe grid so that each possible Tic-Tac-Toe added up to the same number, 15. Three rows, three columns and two diagonals.
It wasn’t easy. Mainly I remembered the method that the book gave for solving the problem, and for many years I had a smug feeling that I knew everything about Magic Squares. This is the method I learned.

(1) Enter the numbers in order.
1 2 3
4 5 6
7 8 9
 (2) Swap the corners.
9 2 7
4 5 6
3 8 1
 (3) Then move every number clockwise around the center number, 5.
4 9 2
3 5 7
8 1 6
 Done! I didn’t even think about higher order Magic Squares (4x4, 5x5, 6x6, etc.) until I picked up a book by Jim Moran called THE WONDERS OF MAGIC SQUARES. It seemed impossibly complex at first, but it soon began to sink in and before long I was saying “Aha!” on practically every page.
Magic Squares, and how to make them, have been studied by mathematicians and puzzlers for more than 2000 years. They wanted “algorithms”, or step-by-step methods, for constructing them. Moran’s book lists fifteen or so methods, one of which I adapted for the Run It program for this feature.
The earliest mention of Magic Squares was in China and India, where they were often carved on stone houses, or on pendants and amulets. “Magic” is a very good name for these squares, at least in a historical sense.
Benjamin Franklin was a Magic Square freak, and often bragged that he could construct 8x8’s as fast as he could write the 64 numbers. Obviously, he knew an algorithm or two.
Generally, Magic Squares are divided into two categories, odd-order and even-order. I’m going to talk about odd-order squares (3x3, 5x5, 7x7, 9x9, etc.) only, because their algorithms will work for any size, while even-order squares require different algorithms, depending on their size.
Each size of square has a constant, which is the number that all rows, columns and diagonals add up to. The formula for this constant is
Constant = (n * (n*n+1))/2

One Magic Square is actually four if you think about it, since you can simply rotate the whole square three times. But there are also many other ways to completely rearrange the numbers to form totally new squares. Would you believe that there are an estimated 275 MILLION (!) possible 5x5 squares?
The algorithm used in the Run It program is a fairly sophisticated method, developed by Jim Moran. It’s based on an early method that involves constructing two squares that aren’t magical, then adding them together to make a square that is. Run the program to see this method explained.
Most of the other methods involve what is known as the “staircase method”. This method will create one Magic Square, which can be changed into other squares by a little manipulation, such as rotation.
Here are the rules for the “staircase method” for a 5x5.

(1) Place the first number, 1, in the middle box of the first row.
(2) Place each succeeding number in the box diagonally upward to the right, unless this box is occupied by a previously placed number.
(3) When the box is “blocked”, place the next number in the box directly below the last number placed.
(4) Keep going till the square is filled.

The first question is, “Where do I put 2? Upwards to the right of 1 is off the grid!” This is what you do. Whenever a box you want to go to is off the grid, imagine that there’s another grid right next to the main one. In the rules above, 2 would be placed in the 5th row, 4th column of that imaginary grid. Instead, place the number 2 in the 5th row, 4th column of your main grid. If it’s blocked, then follow rule (3).
This means that 2 goes in row 5, column 4; 3 goes in row 4, column 5; 4 goes in row 3, column 1, etc. Here is the final grid constructed by this method.
  
17  24  1   8   15
23  5   7   14  16
4   6   13  20  22
10  12  19  21  3
11  18  25  2   9
 The first blocked move came when you tried to place the 6. The 1 was in the way. So 6 goes below 5. Study this till you see the pattern.
There are several variations on the “staircase” method. Here are the rules for one variation.

(1) Place the first number, 1, in the box to the right of the center box.
(2) Fill in the boxes by advancing diagonally upward and to the right. (Same as the other method.)
(3) In case of a blocked move, place the number in the box TWO PLACES TO THE RIGHT of the last number placed.
A blocked move will occur every five numbers. The resulting square will look like this.

3   16  9   22  15
20  8   21  14  2
7   25  13  1   19
24  12  5   18  6
11  4   17  10  23
 Notice that in both of the squares we’ve made, there is a pattern formed by numbers that add up to 26. Number 1 is diametrically OPPOSITE to 25. 2 is OPPOSITE to 24. 3 is OPPOSITE to 23, and so on.
Another way of looking at this is that a Magic Square is “balanced”. If we were to place weights corresponding to the numbers in each box and attach a chain to the middle of the middle box, we could lift the whole square and it would balance about that point. This is only true for squares made with the “staircase” method.
There are a couple of quick ways to make variations on a square, once you’ve made it (besides rotation).

(1) Swap Column 1 with Column 5. Then swap Row 1 with Row 5.
(2) Swap Rows 1 and 2 with Rows 4 and 5. Then swap Columns 1 and 2 with Columns 4 and 5.

There are several more algorithms for constructing Magic Squares which I don’t have the room for, including variations on the algorithm used in the Run It program.
I heartily recommend Jim Moran’s book, THE WONDERS OF MAGIC SQUARES, published by Vintage Books, a subsidiary of Random House, New York, 1981. It contains more than you’ll ever want to know about Magic Squares.
Maurice Kraitchik’s MATHEMATICAL REC­REATIONS, published by Dover Books, is also a great source for Magic Square lore.

Back to 2015. I think a program that generates thousands of magic squares is pretty neat. It shows what a computer is good at if programmed correctly. But I can imagine you wondering if I will ever get to any really useful programs in this project.

The answer is no. I never programmed anything you might call useful. But please stick with me and I think you will find some very interesting stuff coming up. I was a puzzle magazine nut in those days and the C-64 was the perfect platform for turning pencil puzzles into interactive, ego-less, record-keeping tools for solving, or in some cases, generating puzzles. Really! It gets better.

In fact, tomorrow night's program could be considered useful if you are like me and take a half dozen pills every morning and night. Tune in and drop out.