BASM (Basic ASseMbler) Part 1

Feel free to share: Tweet about this on Twitter
Share on LinkedIn
Share on Facebook
Share on Google+

An inline Basic Z80 assembler for CPC464 and later home computers:

During and in the week before our vacation in Scotland last 2 weeks, I have been reading the book: ‘Atari Age The Emerging of Video Games in America’ from Michael Newman.

This is an excellent book about the early days of video games, focussing mostly on the cultural impact of video games and how they were perceived by society in the late 70’s and early 80’s.

Being a child of the 80’s myself, this brought back many fond memories of growing up in this exciting era.

These memories got me thinking about how much fun it was to play with those early 8bit computers. They were so primitive and limited, but at the same time it was much easier to get something done, programming in Basic, than it is now in our high level languages and complex IDE’s. Of course this is due for a big part to the fact that you could do so much less.
You just turned on your computer and typed in some Basic and you were off to the races!

Anyway, reading and remembering, a silly plan started to form in my head, two days ago, to spend some left over evening hours and morning, when it was raining, to do some old fashioned Basic coding using a CPC464 Emulator (WinApe).

A trip down memory lane

When I was around 12-14 years old, we had a Amstrad/Schneider CPC464 8bit home computer.

This was our second real home computer replacing our Philips P2000T (which my parents bought around 1981) and it was a great upgrade.

Our very first ‘computer’ was an Atari 2600, which got me truly hooked on video games and computers in general.

It was on our P2000T, that I first learned how to program in Basic. This machine didn’t really have commercial software. Mostly all available software was made by members of the P2000T User Group (P2000 GG) of the HCC (Hobby Computer Club) of which we (my parents) were members. Every month or so they would release a tape (those small dictaphone tapes) with around 4-6 programs, if I remember correctly).
Long story short; if you wanted to do something on the P2000 (besides playing very primitive text-mode games), you had to code it yourself.

On our CPC464, I continued programming in Basic (now having access to graphics modes and pretty powerful drawing commands). At some point, I got interested in Z80 assembler. Basic is pretty slow and in order to program games and more advanced graphics, assembler was the way to go.

There was only one problem: I had no assembler. And I didn’t know how to get my hands on one. In those days, of course, there was no internet. We got all our information from magazines and computer club meetings.
At some point I did manage to get my hands on a book about Z80 assembler programming. This included detailed discussion on the Z80 instruction set, but also how to calculate the ‘opcodes’ by hand, using binary notation and long tables of the different components that go into calculating opcodes for each instruction variation.
Based on what I learned from this book, I would write machine language programs using the following method:

  1. Write down the program in assembler on paper.
  2. Use the book with all its tables to calculate the binary opcode
  3. Convert the binary opcodes into hexadecimal or decimal numbers
    Calculate the addresses where each instruction’s opcode would go (so you could jump to them, to create loops and control logic).
  4. ‘Poke’ those number into memory
  5. Call the program.

This was a very arduous process. But when it worked and something showed up on screen, or the appropriate sound came out of the computer, it gave me an incredible sense of accomplishment.

However, I knew I needed to get a programmers calculator in order to calculate the (hexa)decimal values of the binary opcodes a bit quicker. I don’t know anymore if I got it from my parents or bought it myself, once I was the proud owner of my calculator my assembler coding went a little faster.

What I really needed next, was an actual assembler to do this work for me.

Then some day, in one of the magazines I read, I found a full listing of a very peculiar solution to my ‘assembler problem’.
This listing was a program that allowed you to write your assembler program in ‘rem’ lines (comment) in the first 0 to 9999 lines of basic code.

The assembler program itself started at line 10000 (or so) and would ‘peek’ the assembler program, byte by byte, line by line from the memory addresses that stores Basic programs. Then it would parse the lines, calculate the opcodes, poke them into memory and in the end call the resulting program.

To me, this was pure magic! So after a long time of typing in the entire listing, making and correcting many mistakes, I finally was the proud owner of my own true assembler. Now I could really focus on the actual code and let the computer do what it was for.

Today, this solution may sound very strange and perhaps even silly. But the CPC464 didn’t have a harddrive, it didn’t even have floppy drives, it only had a tape recorder. So in order to use a software assembler, you’d first have to load the assembler from tape. Then run it, type in the assembler, save it preferably to another tape, run the assembler, save the resulting binary to yet another tape, then reset the computer and run the program from this tape.
Compared to this process this little basic assembler was actually pretty practical. You just loaded the assembler basic program, use the basic editor of the CPC464 (basically the ‘shell’), type ‘run’ and you had a running program (or a crashed computer a lot of the time).
Of course there was no debugging, no syntax highlighting, just very basic error messages, a working program or a crash (or otherwise unwanted result).
But compared to my original manual process it felt like heaven!

I never wrote any big functional programs in assembler on the CPC464 (I kept using Basic for ‘real’ programs), but it allowed me to tinker with sprite routines (the CPC didn’t have hardware sprites like the C64), scrolling playfields, etc.

I also ended up adding functionality to the assembler that allowed me to use labels for easier jumping.

It also taught me a lot about how the very low level details of a computer worked.

Later when we got an Amiga 500, I continued to program in assembler, but now in 68000 assembler, which was another big improvement; oh, all those registers and division and multiplication instructions! Around that time I also started programming in C.

Anyway, all this came back to me while reading that book on video games. And with long days outdoors (and in castles) in the Scottish Highlands, I decided to try to reproduce this basic assembler, and thus BASM was born!

I set myself the ‘modest’ goal of writing enough functionality to assemble a simple program and run it, before the vacation was over. I would use WinApe to emulate the CPC464, as this has a built in debugger (and even a complete assembler) and functionality to get basic code into and out of the emulated computer.

All this would have to be completed in some left-over evening hours (in stead of reading a book), as spending most of the day visiting castles, climbing mountains and dining out had the highest priority of course.

In Part 2, I’ll describe what I ended up building and how in some detail:

Reproducing a Z80 assembler in Basic from 30+ year old memory