Forem

Alexey Medvecky
Alexey Medvecky

Posted on

Embarking on an 80s Time Travel Adventure: Commodore 64 Machine Code Programming with BASIC

In this article, we delve into the captivating realm of Commodore 64 programming, journeying back to the 80s to uncover the art of crafting machine code using the trusty BASIC language.

Unveiling the Challenge

As alluded to in a prior piece, my quest led me to an assembly book, reigniting my quest for knowledge.

The first thing that impressed me was the ability to memorize machine codes and then call them for execution using only BASIC. While this may seem commonplace to some, it was a revelation for me. In an era where disk drives and assemblers weren’t always at hand, BASIC stood as a steadfast companion.

Harnessing BASIC Magic

To showcase BASIC’s prowess, I’ll guide you through crafting a “HELLO WORLD” program in machine code, and the method behind the magic is surprisingly simple.

Key BASIC Commands:

  • PEEK(): Retrieves memory cell contents at a given address.

  • POOKE , : Writes a value to a specific memory cell.

  • READ, DATA: Creates and sequentially reads an array of data.

Our initial venture involves writing a character to video memory, achieved through a concise program structure.

The BASIC part here acts as a program loader in machine codes.

  • Employing the READ command, we extract machine code values one by one until completion.

  • The POKE statement diligently transfers these segments to memory, starting at address 49152 with incremental offsets.

Upon completing the loop, a SYS instruction calls the stored machine code procedure, initiating execution. The machine code itself is elegantly straightforward. We inscribe character code “x” (24) into the accumulator, preserving it at the video memory’s onset (address 1024).

Our exploration doesn’t halt here. The process extends to outputting entire words, data transfer from BASIC to machine code, and dynamic memory interaction.

I can also transfer data from BASIC to the program in machine codes.

Here, the letter code is requested via INPUT and then, via POKE, the resulting value is written to a specific memory cell.
The machine code program takes data from this memory location.

Here is such programming in machine codes straight from BASIC.

A Journey of Rediscovery

Reflecting on my initial tryst with BASIC on a Z80-based home computer, I’ve come to respect its capabilities anew. BASIC’s techniques seamlessly transition to assembly programming, and the language itself emerges as a potent tool, especially through the methodologies detailed in this article.

The Continuation of an Odyssey

Armed with newfound insights, I’m poised to continue my 80s odyssey. The quest persists as I delve deeper into the potential of C64 and Assembly, particularly in tackling intriguing mathematical challenges.

Join me as we traverse the enthralling landscape of 80s programming, unearthing gems from the past to enrich our present journey.

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay