Lab - 1: Exploring 6502 Assembly

Exploring 6502 Assembly: A Journey into Low-Level Programming 

Introduction

Assembly language has always been both fascinating and challenging for programmers. Unlike high-level languages where abstractions make coding easier, assembly requires a deep understanding of how the CPU processes instructions. In this lab, we explore the basics of 6502 Assembly Language through bitmap displays, code modifications, performance enhancements, and using the 6502 Emulator. This experience is the first step towards learning more complex architectures such as x86_64 and AArch64.

Setting Up the 6502 Emulator

The first step in this lab was setting up the 6502 Emulator, which is available at http://6502.cdot.systems. Since the emulator does not save progress automatically, I followed the recommended practice of periodically saving my work. Using Git for version control proved to be an efficient way to manage my code changes and experiments.

Understanding the Bitmap Code

]The provided bitmap code fills the emulator’s display with a solid yellow color. Here’s the breakdown of how it works:

This loop iterates through the display memory, coloring each pixel yellow.

Performance Analysis

Execution Time Calculation

Given a 1 MHz clock speed, I calculated how long it takes for the program to execute:

  • The main loop executes 256 times per page.

  • Since there are six pages to fill, the loop runs 256 × 6 = 1536 times.

  • Each iteration consists of five key instructions (STA, INY, BNE, INC, LDX, and CPX), each with different cycle counts.

  • Summing up the cycles for each iteration and multiplying by 1536 gives the total execution time.

After careful calculations, the execution time was determined to be ~24.576 milliseconds.


Overall, the execution time calculation

  • Clock Speed: 1 MHz
  • Cycle Time: 0.000001 seconds (1 µs)
  • Execution Time: 24,408 cycles × 0.000001 s = 24.408 ms

Memory Usage Calculation

The memory footprint of the software consists of:
  • Program instructions (a few dozen bytes)
  • Pointer variables ($40 and $41)
  • Screen memory of 1.5 KB ($0200 to $0600)
The application works well even on older 8-bit platforms due to its minimal memory footprint.

 Optimizing Performance

I modified the loop to reduce unnecessary instructions and increase memory access to speed up performance. The optimized version runs almost twice as fast by using unrolled loops and STX/Y addressing for better pixel filling. The revised code is as follows:


This reduces instruction cycles and speeds up execution significantly.


We’ve taken our first steps into the fascinating world of 6502 assemblies, unravelling its core concepts and writing our very first program. But this is just the beginning! In the next post, we’ll dive deeper, modify our code, and experiment with new instructions to truly understand the power of low-level programming. Stay tuned—things are about to get even more exciting!

Comments

Popular posts from this blog

Project Stage 3: Enhancing GCC Pass for Multiple Function Clone Handling: Progress Update

Project Stage 1: Troubleshooting a bit - Coming soon