Stephen Smith's Blog

Musings on Machine Learning…

Koch Snowflake in Applesoft Basic

leave a comment »


Introduction

After learning to program in high school in BASIC on a PDP-11, I went to University where first year computer science was taught using WATFIV (Waterloo Fortran IV), where we had to write our programs by hand, type them into an IBM card punch machine and then submit our stack of punch cards to the data center to run. Later we would receive back our punch cards along with any printout from the program.

Definitely a step backwards from the interactive programming on the PDP-11. I joined the co-op program and after my first work term, used much of the money I made to buy an Apple II+ computer. This returned me to BASIC programming with Applesoft BASIC, which was created by Microsoft for Apple to replace their original Integer Basic.

Last time I produced a Koch Snowflake in QB64, a version of BASIC more advanced than the early BASICs like on the PDP-11 or Apple II. QB64 had functions, subroutines, structured programming and line numbers were optional. I wondered about producing the same program on a more original version of BASIC.  Applesoft is a good candidate as it uses line numbers, has no structured programming, but it does have a handful of builtin graphics commands that allow us to write our Koch Snowflake program. The main challenge is what to do about the recursive function calls.

Apple II Emulators

Although I sold my Apple II+ many years ago, to buy an IBM AT clone, it turns out many people have put a lot of effort into writing Apple II emulators and that you can find an Apple II emulator on most systems, In this case I used the AppleWin emulator, which was easy to install and up and running in no time. Below is the Koch Snowflake on the Apple screen. This is in the Apple II’s high resolution graphics mode of 280×192 in 6 colors. Rather low res by today’s standards.

Applesoft BASIC programs aren’t stored on the disk as regular text files, they are stored in a tokenized format which helps the interpreter run them faster. This is a problem as you can’t easily transfer the files back and forth to a PC. Since the program is small, I just typed it into the Apple, but it means to show the listing you get the following screen shots rather than text in the article.

The big innovation of Applesoft BASIC is that variables are floating point by default rather than integer, so this works well for the couple of calculations we need to do. The other thing is that variable names can be longer, but only the first two characters are used for uniqueness, hence AB = ABC = ABCD. The program roughly follows the same logic as the program in the last article, except no functions and recursion is handled by a simple stack stored in a couple of arrays.

The Program

The first part of the program initializes the variables, defines the arrays that will be used to stack level and line length and sets the graphics mode and color (white = 3).

The next part corresponds to the KochSnowflake subroutine in the last article that draws the main three sides of the snowflake.

Lines 200 to 330 correspond to the SnowflakeSide subroutine, note the lack of structured programming forces us to have four IF statements.

Lines 1000 to 1060 are the turtle graphics routine. We don’t have a turn routine, as the angle is a global variable. Move is similar to the Move Subroutine in the last article.

All in all, still quite a simple program. Editing programs in Applesoft is quite old school, but the memory of how to do things returns fairly quickly. Still people who’ve only used graphics editors might find it baffling.

Summary

It’s great that people have put so much effort into creating such high quality emulators of historical computer systems. This was a nice journey back down memory lane of how I did most of my programming in University from second year onwards. The Apple emulators support Apple’s Pascal system and even the add-on Z80 CPM cards. Perhaps I wouldn’t want to produce modern commercial software this way, but I think there are lessons to be learned from learning some of the old ways.

Written by smist08

September 17, 2022 at 9:48 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: