Embarking on an 80s Time Travel Adventure: Commodore 64 Machine Code Programming with BASIC
Tags: commodore64, retrocomputing, assemblylanguage, basic
Published: 2023-08-07 10:43:18

---
title: Embarking on an 80s Time Travel Adventure: Commodore 64 Machine Code Programming with BASIC 
published: true
description: 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. 
tags: #commodore64, #retrocomputing, #assemblylanguage,#basic
# cover_image: https://cdn-images-1.medium.com/max/3200/1*_Q-7il1c-QPvkBVe7JANCQ.png
# Use a ratio of 100:42 for best results.
# published_at: 2023-08-07 10:39 +0000
---
![](https://cdn-images-1.medium.com/max/3200/1*_Q-7il1c-QPvkBVe7JANCQ.png)

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](https://dev.to/medvecky/80s-time-travel-commodore-64-math-with-square-root-approximation-factorization-using-assembly-4chg), my quest led me to an assembly book, reigniting my quest for knowledge.

![](https://cdn-images-1.medium.com/max/2000/1*oDsCoaqk6MaIC_IFNlS8Tg.jpeg)

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(<memory address>): Retrieves memory cell contents at a given address.

* POOKE <memory address>, <value>: 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.

![](https://cdn-images-1.medium.com/max/2000/1*wyTYxBn4CIAIYF3bJYyEdA.png)

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.

![](https://cdn-images-1.medium.com/max/2000/1*6y8iZS3iJBckdupnq8tbrw.png)

![](https://cdn-images-1.medium.com/max/2000/1*KqesFCRv7e-_bSXXKXXlAg.png)

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

![](https://cdn-images-1.medium.com/max/2000/1*Slbw8b85-vmXHJsftvorLA.png)

![](https://cdn-images-1.medium.com/max/2000/1*n64QRHSOPlTxRR3ubfh7bQ.png)

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.

![](https://cdn-images-1.medium.com/max/2000/1*Otr95E_OjKkrefNsnG1aVQ.png)

Here is such programming in machine codes straight from BASIC.

### A Journey of Rediscovery

![](https://cdn-images-1.medium.com/max/2048/1*9DNMeb-U_5C47QKm1jSNSA.png)

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

![](https://cdn-images-1.medium.com/max/2048/1*HoKiI5VcG1nBObdclOj7qQ.png)

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.