Stephen Smith's Blog

Musings on Machine Learning…

Back to Basics

with 4 comments


Introduction

I originally learned to program in grade 10, where my highschool had a DEC LA36 DECwriter terminal connected through a 300 baud modem to a local college’s DEC PDP-11 running the RSTS-11 operating system. We wrote programs in the built-in BASIC programming language. This was 1976 back before structured programming took off where programs had line numbers and you used GOTO statements all over the place. The advantage of BASIC is that it is simple to learn and the students at our high school picked it up pretty quickly. We wrote programs to perform scientific calculations and wrote all sorts of games. Programming back then was a lot of fun. I’ve blogged about quite a few programming languages, including Python, Fortran, Assembly Language, Erlang, Julia, Scratch, Processing, and C. I was just remembering BASIC and wondered about playing around with that. In my career I worked quite a bit with Microsoft’s Visual Basic, but wanted something more old school and open source. Similarly VB.Net is just C# without the semi-colons. After a bit of research, I found there are dozens of implementations of BASIC floating around. Some require line numbers, some contain structured programming, some are interpreters, some are compilers. I may survey some of these in a later blog, but as an example I thought I’d play with QB64 abit which is a compiler implementation of QBasic/QuickBasic. This implementation runs on Windows, Linux and MacOS, but as a compiler I think it only emits Intel/AMD assembly code. I ran this on Ubuntu Linux 22.04 on my laptop.

Koch Snowflake Yet Again

I use drawing the Koch Snowflake as a good example, as it’s a simple program, does something nice, but is long enough to demonstrate a few things of the language. QB64 has lots of built in graphics commands, so why not. The full listing is at the end of the article.

QB64 comes with a character based IDE which is good enough for basic development. Of course the source files are text files, so you can edit them in any editor you prefer. I was worried about the implementation, as I usually use recursion and wasn’t sure if this would be supported, but it turned out this wasn’t a problem. QB64 could easily handle the task and the program is actually fairly compact.

Notes on QB64

  • Installation: I installed QB64 on Ubuntu Linux, the Linux version of QB64 has a shell script setup_lnx.sh which installs all the dependent packages along with the QB64 program. The script seems to know about quite a few flavors of Linux, but I can only attest to it running well on Debian based distributions.
  • Global Variables: I was expecting any variables to be defined or used outside a sub or function to be global, however this isn’t the case you have to “DIM SHARED” any variables you want to be global.
  • Variable Types: By default variables are single precision floating point. If you need them to be something else then you can use a “DIM” statement or there are a number of special characters that when placed on the end of a variable specify the type. Hence the & at the end of WHITE& tells QB64 this is a long integer. If you leave this off the program won’t work as the value produced by _RGB32(255, 255, 255) doesn’t fit in a floating point variable.
  • Line Numbers: This is a newer flavor of Basic with structured programming and hence no line numbers. You can always add labels and GOTO statements if you want to.
  • There is a JavaScript version of the compiler that emits JavaScript code, but it didn’t work on this program, generating JavaScript with a syntax error which I think was to do with the recursion. However, playing with QB in the JavaScript sandbox is fun.

Summary

QB64 is fun to play with. It’s not as old as the RSTS-11 Basic I learned in school and is a bit newer than the AppleSoft Basic I played with on my old Apple II+. If you worked with either QuickBasic or GWBasic on MS-DOS then this will be familiar, including the character based IDE.

In education, kids seem to start programming in Scratch and then progress to programming in Python. I wonder if there is room for Basic in-between as something more substantial than Scratch but without the complexity of the modern Python language. You can do a lot with Basic without the steep learning curve associated with more modern systems.

‘ Program to draw a Koch snowflake
_Title “Koch Snowflake”

STARTX = 50
STARTY = 130
STARTANGLE = 0
LEVEL% = 3

DECLARE SUB KochSnowflake(level%)
DECLARE SUB SnowflakeSide(level%, size%)
DECLARE SUB move(dist%)

Dim Shared X, Y, angle, PI, WHITE&

WHITE& = _RGB32(255, 255, 255)
PI = 3.14159
X = STARTX
Y = STARTY
angle = STARTANGLE

Screen _NewImage(520, 520, 32)
Cls
KochSnowFlake (LEVEL%)

Sub SnowflakeSide (level%, size%)
    If level% = 0 Then
        move (size%)
    Else
        Call SnowflakeSide(level% – 1, size% / 3)
        turn (60)
        Call SnowflakeSide(level% – 1, size% / 3)
        turn (-120)
        Call SnowflakeSide(level% – 1, size% / 3)
        turn (60)
        Call SnowflakeSide(level% – 1, size% / 3)
    End If
End Sub

Sub KochSnowFlake (level%)
    turn (60)
    Call SnowflakeSide(level%, 400)
    turn (-120)
    Call SnowflakeSide(level%, 400)
    turn (-120)
    Call SnowflakeSide(level%, 400)
End Sub

Sub move (dist%)
    oldx = X
    oldy = Y
    X = X + dist% * Cos(angle * PI / 180)
    Y = Y + dist% * Sin(angle * PI / 180)
    Line (oldx, oldy)-(X, Y), WHITE&
End Sub

Sub turn (angle_increment)
    angle = angle + angle_increment
End Sub

Written by smist08

September 4, 2022 at 3:42 pm

Posted in programming

Tagged with , ,

4 Responses

Subscribe to comments with RSS.

  1. Very nice post! My first opportunity was my freshman year at college in 1969. As an engineering major we were introduced to Fortran. BASIC wasn’t yet available. We wrote our programs on Fortran code forms and then typed them into a punch card printer. We would hand the stack of cards to a tech in a window. He would feed them as a batch into the U of Florida data center (Northeast (FL) Reginal Database Center (NERDC). We would get the printout back the next day. You didn’t want to put your weekly project off to the last minute! 😉

    Fos13

    September 4, 2022 at 4:52 pm

    • When I went to University we did 1st year computer science on punched cards in Watfiv (Waterloo Fortran). Definitely a step backwards from high school.

      smist08

      September 4, 2022 at 5:19 pm

  2. […] with my series on ancient versions of the BASIC programming language, this week we’ll look at GW-Basic which was […]

  3. […] Read More […]


Leave a comment

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